ArrayList集合的一些用法

2021-07-11 17:16:24 字數 791 閱讀 2811

首先要引用命名空間

using system.collections;

引用了此命名空間,才可以用下面的語句宣告物件

arraylist list = new arraylist();//宣告乙個arraylist的物件,物件名為list

1.新增單個元素,新增的元素可以為任意型別

list.add(true);

list.add(1);

list.add("張三");

2.新增集合元素

list.addrange(new int );

3.arraylist常用的一些方法

清空所有元素

list.remove(true );//刪除單個元素,寫誰就刪誰

list.removeat(1);//根據下標去刪除元素

list.removerange(1,2);//根據下標移除一定範圍的元素

list.reverse();//反轉元素

list.insert(1, false);//在指定的索引處的元素之前插入乙個元素

list.insertrange(0, new string );//在指定的索引處的元素之前插入乙個集合

bool b = list.contains(1);//判斷是否包含乙個指定的元素

一些ArrayList的一些小方法

1 arraylist的一些小方法 加入某本圖書 list.add bk5 檢視是否包含某本圖書 boolean a list.contains bk1 system.out.println a 刪除某本圖書 list.remove bk5 根據圖書名檢視,刪除該圖書 集合的下角標是從0開始的 fo...

一些特殊的集合

1.stack集合 棧 stack,先進後出,乙個乙個賦值,乙個乙個取值,按順序。屬性和方法 count 取集合內元素的個數 push 將元素乙個乙個推入集合中 pop 將元素乙個乙個彈出集合 clear 清空集合 2.queue 佇列集合 先進先出,乙個乙個的賦值,乙個乙個的取值,按照順序。屬性和...

簡單模擬ArrayList的一些方法

include include struct arr void init arr struct arr int 初始化 void insert arr struct arr 插入 void delete arr struct arr 刪除 int get bool is empty struct a...