当前位置:网站首页>Use of ArrayList
Use of ArrayList
2022-07-24 07:44:00 【Glunn】
using System;
using System.Collections;//arraylist The header file
using System.Collections.Generic;//list The header file
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication63
{
class fathder
{
}
class Program
{
ArrayList arrayList = new ArrayList();
List li = new List();// Definition list You have to define when list The type of ,arraylist no need
static void Main(string[] args)
{
arrayList.Add(10);// Any type can be specified when passing parameters inside
arrayList.Add(“nihao”);
arrayList.Add(new fathder());
foreach (var item in arrayList)// It can be used foreach facilitate ArrayList All elements are treated as object, So you need to force type conversion when accessing elements
{
Console.WriteLine(item);
}
li.Add(5);// After specifying the type, only parameters of the specified type can be passed List<T> Ensure the security of the type . There is no need for type conversion when getting elements
Console.ReadKey();
}
}
}
ArrayList Class is a special array . It comes from System.Collections Namespace ; By adding and deleting elements , You can change the length of the array dynamically .
ArrayList initialization ArrayList There are three kinds of initialization
1) Do not initialize capacity ArrayList al = new ArrayList();// Default capacity is 0, When the array capacity is full, the array will automatically change to the current array capacity 2 Double expansion
2) Initialize capacity ArrayList al = new ArrayList(3);// The initial capacity is 3
3) Initialize with a set or array ArrayList al = new ArrayList(a);//a For a collection or array
Additive elements
ArrayList arrayList = new ArrayList();
arrayList.Add(10);// Any type can be specified when passing parameters inside
arrayList.Add(“nihao”);
arrayList.Add(new fathder());
foreach (var item in arrayList)// It can be used foreach facilitate ArrayList All elements are treated as object, So you need to force type conversion when accessing elements
{
Console.WriteLine(item);
}
Remove elements
arrayList.Remove(object obj);// Remove... From the array obj Elements
arrayList.RemoveAt(int index);// Remove index to index Digital elements of
arrayList.RemoveRange(int indext,int count);// Remove from index index Start , remove count Elements
Look for the element
Find elements with Contains()、IndexOf()、LastIndexOf()3 Medium method
al.Contains(object obj);// Find whether there is... In the array obj Elements , There is returned true;
IndexOf() There are two overload methods Usage is as follows :
1)、al.IndexOf(object obj);// from 0 Start looking for obj Elements , Only the first obj Elements , And return the position in the array , If it doesn't exist , return -1;
2)、al.IndexOf(object obj,int startIndex); // from startIndex Start looking for obj Elements , Only the first obj Elements , And return the position in the array ,
3)、al.IndexOf(object obj,int startIndex,int count); from startIndex Start thinking and find count Elements , If there is obj Elements , Returns its position in the array
al.LastIndexOf() Methods and IndexOf() The usage is the same , It also has two overloads , The difference is ,LastIndexOf(obj) Yes find to obj The last place
ArrayList The traversal
Count Property can be retrieved ArrayList Length of array , It can be used for Traversal array
for (int i = 0; i < al.Count;i++ )
{
Console.Write("{0}\t",al[i]);
}
use foreach Traverse
foreach(object o in al)
{
Console.Write("{0}\t", o);
}
边栏推荐
- Train-clean-100 dataset
- 23. Component customization events
- Simple Gateway - intranet server safely obtains external network data
- FlinkSQL-UDF自定义数据源
- Log in to the server using the fortress machine (springboard machine)
- Advanced part of C language VI. file operation
- Collection of binary tree topics
- HCIP第七天
- 【信息系统项目管理师】第七章 复盘成本管理知识架构
- Mutual implementation of stack and queue (c)
猜你喜欢

Jackson parsing JSON detailed tutorial

Harbor2.2 用户角色权限速查

Stable TTL serial port rate supported by Arduino under different dominant frequencies

HCIP第七天

About the solution of thinking that you download torch as a GPU version, but the result is really a CPU version

Problems encountered in inserting large quantities of data into the database in the project

Harbor2.2 quick check of user role permissions

Advanced part of C language VI. file operation

XSS漏洞学习

System integration project management engineer (soft test intermediate) key knowledge, recitation version
随机推荐
MS SQL Server 2019 学习
Introduction to C language I. branch and loop statements
【sklearn】tree.DecisionTreeClassifier
2021-06-03 database query - sorting
C语言文件操作
Harbor2.2 用户角色权限速查
【Pytorch】conv2d torchvision.transforms
GBK code in idea is converted to UTF-8 format ctrl+c+v one second solution perfect solution for single code file escape
Appium doctor command error pit - resolved
【sklearn】PCA
hcip第八天笔记
Deep learning two or three things - review those classical convolutional neural networks
Mitre att & CK ultra detailed learning notes-01 (background, terms, cases)
2021-06-03pip error valueerror: unable to find resource t64.exe in package pip_ vendor.distlib
Sword finger offer special assault edition day 8
Automatic test and manual test
Anaconda install pytorch
Service Vulnerability & FTP & RDP & SSH & Rsync
Selenium basic knowledge automatic login QQ space
Introduction to C language