1、ArrayList集合
它不是静态类,所以可以new一个对象,创建集合对象:
ArrayList list = new ArrayList();
由于数组长度不可变,类型单一;
集合的好处:长度任意改变,类型随便;
对于数组,把命名空间打印出来:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayList集合
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(3.141592653);
list.Add(true);
list.Add("zhang");
list.Add(500m);
list.Add(new int[] { 1, 2, 3, 4, 5, 6, 7 });
list.Add(new Person());
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey();
}
}
public class Person
{
public void PersonSay()
{
Console.WriteLine("This Is Person");
}
}
}
打印里面的所有内容:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayList集合
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(3.141592653);
list.Add(true);
list.Add("zhang");
list.Add(500m);
list.Add(new int[] { 1, 2, 3, 4, 5, 6, 7 });
list.Add(new Person());
for (int i = 0; i < list.Count; i++)
{
//Console.WriteLine(list[i]);
if (list[i] is Person)
{
((Person)list[i]).PersonSay();
}
else if (list[i] is int[])
{
for (int j = 0; j < ((int[])list[i]).Length; j++)
{
Console.Write("{0} ", ((int[])list[i])[j]);
}
Console.WriteLine();
}
else
{
Console.WriteLine(list[i]);
}
}
Console.ReadKey();
}
}
public class Person
{
public void PersonSay()
{
Console.WriteLine("This Is Person");
}
}
}
2、ArrayList元素的增加
代码简化,使用AddRange:
AddRange是添加集合的方法,Add是添加单个元素的方法。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayList集合
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.AddRange(new string[] { "1", "2", "3" });
list.AddRange(new int[] { 4, 5, 6, 7, 8 });
for (int i = 0; i < list.Count; i++)
{
Console.Write("{0} ",list[i]);
}
Console.ReadKey();
}
}
public class Person
{
public void PersonSay()
{
Console.WriteLine("This Is Person");
}
}
}
3、ArrayList元素的插入
list.Insert(0, 0);插入单个元素
list.InsertRange(0, new int[] { 9, 10, 11 });插入集合
4、ArrayList元素的删除
list.Remove(8);删除单个元素
list.RemoveAt(0);根据索引删除
list.RemoveRange(0,3);根据下标移除一定范围的方法
5、ArrayList元素的清空
list.Clear();清空所以元素
6、ArrayList元素的排序
只有统一的类型才能排序
list.Sort();
7、ArrayList元素的反转
list.Reverse();
8、是否包含
list.Contains(8);返回bool
9、集合长度问题
count实际上包含元素个数
capcity集合中可以包含的元素个数,内存申请开辟多一倍的空间 :4→8→16→32→64…..来保证集合长度够用
10、集合练习
求集合的平均值和总和:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayList集合
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 89, 100 });
int sum = 0;
int max = int.MinValue;
for (int i = 0; i < list.Count; i++)
{
if ((int)list[i] > max)
{
max = (int)list[i];
}
sum += (int)list[i];
}
Console.WriteLine("总和:{0}\n均值:{1}\n最大值:{2}", sum,sum*1.0/list.Count,max);
Console.ReadKey();
}
}
}
写一个长度为10 的集合,要求集合里的数字随机产生,并且互不相同:
方法1:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayList集合
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
int rNumber = r.Next(0, 10);
list.Add(rNumber);
if (i > 0)
{
for (int j = 0; j < i; j++)
{
if ((int)list[i] == (int)list[j])
{
rNumber = r.Next(0, 10);
list.RemoveAt(i);
list.Add(rNumber);
j = -1;//因为返回去时有j++就变成0了,单步调试可以发现
}
}
}
}
for (int i = 0; i < list.Count; i++)
{
Console.Write("{0} ",(int)list[i]);
}
Console.ReadKey();
}
}
}
方法2:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayList集合
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
int rNumber = r.Next(0, 10);
if (!list.Contains(rNumber))//不重复的
{
list.Add(rNumber);
}
else//重复的
{
i--;//重新来过
}
}
for (int i = 0; i < list.Count; i++)
{
Console.Write("{0} ", (int)list[i]);
}
Console.ReadKey();
}
}
}
方法3:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayList集合
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
int rNumber = r.Next(0, 10);
while (list.Contains(rNumber))//死循环
{
rNumber = r.Next(0, 10);
}
list.Add(rNumber);
}
for (int i = 0; i < list.Count; i++)
{
Console.Write("{0} ",(int)list[i]);
}
Console.ReadKey();
}
}
}