ArrayList:
来源:互联网 发布:sig516突击步枪知乎 编辑:程序博客网 时间:2024/06/05 19:34
(1).默认构造器:ArrayList List = new ArrayList();
eg: ArrayList List = new ArrayList();
for (int i = 0; i < 10; i++)
{
List.Add(i);
}
foreach (int m in List)
{
Console.Write(m+" ");
}
Console.ReadLine();
(2)用一个ICollection对象来构造,并将集合的元素添加到ArrayList中
eg:int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ArrayList List = new ArrayList(arr);
foreach (int m in List)
{
Console.Write(m + " ");
}
Console.ReadLine();
(3)用指定大小初始化内部的数组
eg: ArrayList MY = new ArrayList(10);
for(int i = 0; i < MY.Count; i++)
{
MY.Add(i);
}
添加ArrayList元素
(1)Add方法:public virtual int Add(object value)
int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
ArrayList List = new ArrayList(arr);
List.Add(7); //在ArrayList的尾部添加
foreach (int m in List)
{
Console.Write(m + " ");
}
Console.ReadLine();
(2)Insert方法:public virtual void Insert(int index,object value)
index:从零开始索引,应在该位置插入value
eg:int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
ArrayList List = new ArrayList(arr);
List.Insert(3,7); //在ArrayList集合的指定索引处添加元素
foreach (int str in List)
{
Console.WriteLine(str);
}
Console.ReadLine();
删除ArrayList元素
(1)Clear方法:public virtual void Clear()
eg:int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
ArrayList List = new ArrayList(arr);
List.Clear(); //清除ArrayList集合的元素
foreach (int m in List)
{
Console.Write(m + " ");
}
Console.ReadLine();
(2)Remove方法:public virtual void Remove(object obj)
eg:int[] arr = new int[] {1,2,3,4,5,6 };
ArrayList List = new ArrayList(arr);
List.Remove(3); //一处ArrayList集合中与3匹配的元素
foreach (int m in List)
{
Console.Write(m + " ");
}
Console.ReadLine();
(3)RemoveAt方法:public virtual void RemoveAt(int index)
eg: int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
ArrayList List = new ArrayList(arr);
List.RemoveAt(3); //移除ArrayList集合中索引为3的元素
foreach (int m in List)
{
Console.Write(m + " ");
}
Console.ReadLine();
(4)RemoveRange方法:public virtual void RemoveRange(int index,int count)
eg:int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
ArrayList List = new ArrayList(arr);
List.RemoveRange(3,2); //从ArrayList集合的索引3处移除两个元素
foreach (int m in List)
{
Console.Write(m + " ");
}
Console.ReadLine();
利用以上介绍的方法 编程实现:
定义一个teacher 类,包含姓名和职称和一个输出自己信息的方法,建立arraylist类动态数组将3个teacher类对象的增加到数组中,在索引值为1的位置 插入一个 teacher类对象,删除索引值为1 的元素,最后输出数组中 所有元素的信息
可参看 教材 154-157页
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace _1
{
class Program
{
static void Main(string[] args)
{
teacher p1 = new teacher("张三1","软件工程师1" );
teacher p2 = new teacher("张三2","软件工程师2");
teacher p3 = new teacher("张三3","软件工程师3");
ArrayList aList = new ArrayList();
aList.Add(p1);
aList.Add(p2);
aList.Add(p3);
aList.RemoveAt(1); //删除下标1的元素,后面元素依次向前移动
for (int i = 0; i < aList.Count; i++) //Count属性确定元素实际个数
{
teacher my = (teacher)aList[i]; //如果数组中存放对象需要进行强制转换
my.show();
}
Console.ReadLine();
}
}
class teacher
{
public string name;
public string worker;
public teacher(string name,string worker)
{
this.name = name;
this.worker = worker;
}
public void show()
{
Console.WriteLine("{0} {1}",name ,worker );
}
}
}
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- 黑马程序员——java中的集合
- Andorid学习【基础篇】Android应用程序生命周期
- 【转载】Z-STACK中关于非易失性存储器Nv操作实例
- uva 439
- JavaScript事件mouseover和mouseenter的区别
- ArrayList:
- 机房收费系统重构——数据库设计
- in-house发布的ipa包,如何触发下载安装
- Android中的子菜单(SubMenu)使用案例
- 方法的分类:
- 高精度模板【高精度加减乘除模带负数判定】
- 索引器:通常用来操作数组中的元素
- Leetcode Kth Largest Element in an Array
- 第38讲-项目二-打豆豆