当前位置:网站首页>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);
}
边栏推荐
- Selenium basic knowledge automatically login Baidu online disk
- Arduino在不同主频下稳定支持的TTL串口速率
- 多种优化方法打印100~200之间的素数
- MS SQL Server 2019 learning
- Advanced part of C language IV. detailed explanation of user-defined types
- Tools for data visualization
- Give a string ① please count the number of times each letter appears ② please print the pair with the most letters
- Selenium use
- 爬虫学习-概述
- About using the alignment function of VMD
猜你喜欢
![[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management](/img/a9/7cdab9264902b1e2947a43463f6b32.png)
[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management

Buffer overflow vulnerability of network security module B in national vocational college skills competition

Selenium basic knowledge paging processing

【sklearn】tree.DecisionTreeClassifier

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

Train-clean-100 dataset

Introduction to C language II. Functions

Arduino在不同主频下稳定支持的TTL串口速率

觉维设计响应式布局

HCIP第七天
随机推荐
Introduction to C language v First understanding pointer VI. first understanding structure
24.全局事件总线
Introduction to C language
Selenium basic knowledge automatic login QQ space
Simple installation of sqli Labs
XSS漏洞学习
23.组件自定义事件
Postman extracts the token parameter value in the response header and sets it as an environment variable, with code attached
Influxdb未授权访问&CouchDB权限绕过
24. Global event bus
Deep learning two or three things - review those classical convolutional neural networks
2022-07-23:给定N件物品,每个物品有重量(w[i])、有价值(v[i]), 只能最多选两件商品,重量不超过bag,返回价值最大能是多少? N <= 10^5, w[i] <= 10^5, v
基于VSCode聊聊编译器那些事儿
【HiFlow】腾讯云HiFlow场景连接器实现校园信息管理智能化
[FreeRTOS] 11 software timer
Simple Gateway - intranet server safely obtains external network data
2022-07-23: given n items, each item has weight (w[i]) and value (v[i]), only two items can be selected at most, and the weight does not exceed bag. What is the maximum return value? N <= 10^5, w[i] <
There are two tables in Oracle, a and B. these two tables need to be associated with the third table C. how to update the field MJ1 in table a to the value MJ2 in table B
Using bidirectional linked list to realize stack (c)
[information system project manager] Chapter VII recheck cost management knowledge structure