通过实例学C#之ArrayList

news2024/10/6 18:28:57

介绍

        ArrayList对象可以容纳若干个具有相同类型的对象,那有人说,这和数组有什么区别呢。其区别大概可以分为以下几点:

1.数组效率较高,但其容量固定,而且没办法动态改变。

2.ArrayList容量可以动态增长,但它的效率,没有数组高。

所以建议,如果能确定容纳对象数量的话,那么优先使用数组,否则,使用ArrayList为佳。


构造函数

ArrayList()

        返回一个capacity属性为0的实例,但capacity为0,不代表其不能内部添加对象,而是会随着对象的增加,而动态改变其capacity属性。

static void Main(string[] args)
{
    ArrayList al= new ArrayList();
    Console.WriteLine(al.Capacity);
    Console.ReadKey();
}

运行结果:
0

ArrayList(ICollection)

        利用一个数组来创建ArrayList实例,实例的Capacity属性为数组的大小。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };

    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine(al.Capacity);
    Console.ReadKey();
}

运行结果:
5

ArrayList(Int32)

        使用一个整形参数来创建一个ArrayList对象,其Capacity等于参数值。

static void Main(string[] args)
{
    ArrayList al= new ArrayList(10);
    Console.WriteLine(al.Capacity);
    Console.ReadKey();
}

运行结果:
10

常用属性

Capacity

        ArrayList对象的容量大小。


Count

        ArrayList对象包含的元素数量。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };

    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine(al.Count);
    Console.ReadKey();
}

运行结果:
5

Item[int32]

        可以通过索引获取指定index的元素值。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };

    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine(al[0]);
    Console.ReadKey();
}

运行结果:
1

常用方法

Add(Object)

        在ArrayList实例的结尾添加一个元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };

    ArrayList al= new ArrayList(arrayInt);
    al.Add(6);

    foreach(int i in al)
    {
        Console.WriteLine(i);
    }
    Console.ReadKey();
}

运行结果:
1
2
3
4
5
6

AddRange(ICollection)

        在ArrayList实例的末尾添加一个数组。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5 };
     ArrayList al= new ArrayList(arrayInt);

     int[] arrayAdd = { 10, 11, 12 };
     al.AddRange(arrayAdd);

     foreach(int i in al)
     {
         Console.WriteLine(i);
     }
     Console.ReadKey();
 }

运行结果:
1
2
3
4
5
10
11
12

BinarySearch(Object value)

        寻找参数value出现在ArrayList实例中的位置,如果实例中不含有value这元素,那么返回一个负数。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);

    int idx=al.BinarySearch(3);
    Console.WriteLine("元素3所在的位置是:"+idx);

    idx=al.BinarySearch(100);
    Console.WriteLine("元素100所在的位置是:" + idx);

    Console.ReadKey();
}

运行结果:
元素3所在的位置是:2
元素100所在的位置是:-6

Clear()

        清除ArrayList对象中的所有元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);

    al.Clear();
    Console.WriteLine("al的元素有:");
    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
al的元素有:

Contains(Object item)

        判断ArrayList实例中是否含有item元素,如果有,返回true,否则,返回false。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);

    Console.WriteLine("al中是否含有元素5?:"+al.Contains(5));
    Console.WriteLine("al中是否含有元素100?:" + al.Contains(100));

    Console.ReadKey();
}

运行结果:
al中是否含有元素5?:True
al中是否含有元素100?:False

CopyTo(int index, Array array, int arrayIndex, int count)

        把ArrayList实例中从index开始的count个元素,复制到array中从arrayIndex开始的元素。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5 };
     ArrayList al= new ArrayList(arrayInt);

     int[] array = new int[3];       //创建一个长度为3的int数组
     al.CopyTo(1, array, 0, 3);

     foreach(int i in array)
     {
         Console.WriteLine(i);
     }

     Console.ReadKey();
 }

运行结果:
2
3
4

FixedSize(ArrayList)

        输入一个ArrayList对象,返回该对象的一个具有固定长度的复制,如果对复制执行Add()操作,则会报错。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);
    
    ArrayList fixAl=ArrayList.FixedSize(al);
    fixAl.Add(6);
    foreach(int i in fixAl)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:


GetRange (int index, int count)

        由ArrayList实例中从index起,共count个元素组成一个新的ArrayList进行输出。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);
    
    ArrayList al2=al.GetRange(1, 3);
    foreach(int i in al2)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
2
3
4

IndexOf (object value)

        返回value值在ArrayLIst实例中的index。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5 };
     ArrayList al= new ArrayList(arrayInt);

     Console.WriteLine("元素5的index为:"+al.IndexOf(5));

     Console.ReadKey();
 }

运行结果:
元素5的index为:4

Insert (int index, object value);

        在ArrayList实例中index位置插入新元素value。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);
    al.Insert(1, 100);
    Console.WriteLine("al的元素有:");

    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
al的元素有:
1
100
2
3
4
5

InsertRange (int index, ICollection c);

        在ArrayList实例中的index位置插入数组c。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 };
    ArrayList al= new ArrayList(arrayInt);

    int[] insertInt = { 100, 101, 102 };
    al.InsertRange(1, insertInt);
    Console.WriteLine("al的元素有:");

    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
al的元素有:
1
100
101
102
2
3
4
5

LastIndexOf (object  value)

        获取元素value最后一次出现的index值。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine("元素1最后一次出现的位置是:"+al.LastIndexOf(1));

    Console.ReadKey();
}

运行结果:
元素1最后一次出现的位置是:5

ReadOnly (ArrayList list)

        输入一个ArrayList参数,返回一个元素与参数一样的只读ArrayList对象。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
    ArrayList al= new ArrayList(arrayInt);
    Console.WriteLine("al的readonly属性为:" + al.IsReadOnly);

    ArrayList readOnlyAl = ArrayList.ReadOnly(al);
    Console.WriteLine("readOnlyAl的readonly属性为:"+readOnlyAl.IsReadOnly);

    Console.ReadKey();
}

运行结果:
al的readonly属性为:False
readOnlyAl的readonly属性为:True

Remove (object obj)       

        清除ArrayList实例中的指定元素obj。如果obj多次出现,那么只清除第一个出现的obj元素。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
     ArrayList al= new ArrayList(arrayInt);
     
     al.Remove(1);
     foreach(int i in al)
     {
         Console.WriteLine(i);
     }

     Console.ReadKey();
 }


运行结果:
2
3
4
5
1

RemoveAt(int index)

        清除index位置上的元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
    ArrayList al= new ArrayList(arrayInt);
    
    al.RemoveAt(1);
    foreach(int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
3
4
5
1

RemoveRange (int index, int count)

        清除ArrayList对象中起始位置为index,长度为count的区域里的元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5 ,1};
    ArrayList al= new ArrayList(arrayInt);
    
    al.RemoveRange(1,3);
    foreach(int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
5
1

Repeat (object value, int count)

        使用count个value元素,组成一个新的ArrayList对象。

static void Main(string[] args)
{
    ArrayList al = ArrayList.Repeat(100, 5);

    foreach(int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
100
100
100
100
100

Reverse ()

        将ArrayList对象的所有元素进行反向排序。

 static void Main(string[] args)
 {
     int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
     ArrayList al = new ArrayList(arrayInt);

     al.Reverse();

     foreach (int i in al)
     {
         Console.WriteLine(i);
     }

     Console.ReadKey();
 }

运行结果:
1
5
4
3
2
1

SetRange (int index, ICollection c)

        将ArrayList实例中从index开始的元素,替换为数组c的元素。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
    ArrayList al = new ArrayList(arrayInt);

    int[] replaceInt = { 100, 101, 102 };
    al.SetRange(1, replaceInt);

    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
100
101
102
5
1

Sort ()

        将ArrayList实例中的元素进行排序。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
    ArrayList al = new ArrayList(arrayInt);

    al.Sort();

    foreach (int i in al)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
1
2
3
4
5

ToArray ()

        将ArrayList对象转换成一个object数组。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
    ArrayList al = new ArrayList(arrayInt);

    object[] array=al.ToArray();

    foreach (int i in array)
    {
        Console.WriteLine(i);
    }

    Console.ReadKey();
}

运行结果:
1
2
3
4
5
1

TrimToSize ()

        将ArrayList对象的capacity属性设置为其实际含有的元素数量。

static void Main(string[] args)
{
    int[] arrayInt = { 1, 2, 3, 4, 5, 1 };
    ArrayList al = new ArrayList(arrayInt);
    al.Capacity = 10;
    Console.WriteLine("al的capacity为:"+al.Capacity);

    al.TrimToSize();
    Console.WriteLine("al的capacity为:" + al.Capacity);

    Console.ReadKey();
}

运行结果:
al的capacity为:10
al的capacity为:6

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1607601.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Go栈内存管理源码解读

基本介绍 栈内存一般是由Go编译器自动分配和释放,其中存储着函数的入参和局部变量,这些参数和变量随着函数调用而创建,当调用结束后也会随之被回收。通常开发者不需要关注内存是分配在堆上还是栈上,这部分由编译器在编译阶段通过…

Day92:系统攻防-WindowsLinux远程探针本地自检任意执行权限提升入口点

目录 操作系统-远程漏扫-Nessus&Nexpose&Goby Nessus Nexpose 知识点: 1、远程漏扫-Nessus&Nexpose&Goby 2、本地漏扫-Wesng&Tiquan&Suggester 3、利用场景-远程利用&本地利用&利用条件 操作系统-远程漏扫-Nessus&Nexpose&a…

C语言(一维数组)

Hi~!这里是奋斗的小羊,很荣幸各位能阅读我的文章,诚请评论指点,关注收藏,欢迎欢迎~~ 💥个人主页:小羊在奋斗 💥所属专栏:C语言 本系列文章为个人学习笔记&#x…

鸿蒙TypeScript学习21天:【声明文件】

TypeScript 作为 JavaScript 的超集,在开发过程中不可避免要引用其他第三方的 JavaScript 的库。虽然通过直接引用可以调用库的类和方法,但是却无法使用TypeScript 诸如类型检查等特性功能。为了解决这个问题,需要将这些库里的函数和方法体去…

将本地项目上传到Github

首先安装git、创建github账号 1、创建一个新的仓库 2、创建SSH KEY。先看一下你C盘用户目录下有没有.ssh目录,有的话看下里面有没有id_rsa和id_rsa.pub这两个文件,有就跳到下一步,没有就通过下面命令创建。 ssh-keygen -t rsa -C "you…

微信小程序echart图片不显示 问题解决

目录 1.问题描述:2.解决方法:2.1第一步2.2第二步2.2效果 小结: 1.问题描述: echart图片不显示 图片: 2.解决方法: 2.1第一步 给wxml中的ec-canvas组件添加宽高样式:style"width: 100%…

Docker容器tomcat中文名文件404错误不一定是URIEncoding,有可能是LANG=zh_CN.UTF-8引起

使用Docker部署tomcat,出现中文名文件无法读取,访问就是404错误。在网上搜索一通,都说是在tomcat的配置文件server.xml中修改一下URIEncoding为utf-8就行,但是我怎么测试都不行。最终发现,是Docker启动时,传…

【经典算法】LeetCode 64. 最小路径和(Java/C/Python3/Golang实现含注释说明,Easy)

作者主页: 🔗进朱者赤的博客 精选专栏:🔗经典算法 作者简介:阿里非典型程序员一枚 ,记录在大厂的打怪升级之路。 一起学习Java、大数据、数据结构算法(公众号同名) ❤️觉得文章还…

java文件夹文件比较工具

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.HashSet; import java.util.Set;public class FolderFileNames {public static void main(String[] args) {// 假设您要读取的文件夹路径是 &q…

代码随想录-算法训练营day12【休息,复习与总结】

代码随想录-035期-算法训练营【博客笔记汇总表】-CSDN博客 ● day 12 周日休息(4.14) 目录 复习与总结 0417_图论-太平洋大西洋水流问题 0827_图论-最大人工岛 复习与总结 二刷做题速度提升了一大截,ヾ(◍∇◍)ノ゙加…

【IDEA】JRebel LS client not configured

主要原因就是因为 jrebel 的版本跟 idea的版本对不上,或者说jrebel的版本比idea的版本还高,导致出现该错误 查看idea版本 ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/a7ba43e6822947318cdb0d0e9d8d65e9.png 获取jrebel 版本 如何处理 …

设计模式:简单工厂模式(Simple Factory)

设计模式:简单工厂模式(Simple Factory) 设计模式:简单工厂模式(Simple Factory)模式动机模式定义模式结构时序图模式实现测试模式分析实例:Qt 控件类优缺点适用环境模式应用 设计模式&#xff…

李沐-26 网络中的网络 NiN【动手学深度学习v2】

主要记载关于全局平均池化层(Global Average Pooling, GAP)中如下两点的理解: 1. GAP的原理 2. 相对于全连接层,GAP具有更少的参数 为了直观地说明全局平均池化层相对于全连接层具有更少的参数,我们可以构造一个简…

博客文章:AWS re:Invent 2023 新产品深度解析 - 第四部分

TOC 🌈你好呀!我是 是Yu欸 🌌 2024每日百字篆刻时光,感谢你的陪伴与支持 ~ 🚀 欢迎一起踏上探险之旅,挖掘无限可能,共同成长! 写在最前面 去年发布文章的一部分,由于内…

bugku-web-login2

这里提示是命令执行 抓包发现有五个报文 其中login.php中有base64加密语句 $sql"SELECT username,password FROM admin WHERE username".$username.""; if (!empty($row) && $row[password]md5($password)){ } 这里得到SQL语句的组成,…

SOLIDWORKS批量改名工具个人版 慧德敏学

每个文件都会有自己的名字,SOLIDWOKRKS模型也不例外。但是如果从资源管理器直接修改模型的文件名,就会导致模型关联的丢失,导致装配体打开之后找不到模型,因此就需要使用SOLIDWORKS的重命名功能。 SOLIDWORKS批量改名插件- Solid…

Blazor 下的 Json 编辑器

最近恰好碰到个比较冷门的需求,就是在线编码 Json,这其中有Json的语法着色,有Json对象属性数据类型的限制,其实要是单纯改一下Json字符串也不是难事,就是没法控制让用户只能给属性值,而不是属性名称&#x…

【随笔】Git 高级篇 -- 远程服务器拒绝 git push reset(三十二)

💌 所属专栏:【Git】 😀 作  者:我是夜阑的狗🐶 🚀 个人简介:一个正在努力学技术的CV工程师,专注基础和实战分享 ,欢迎咨询! 💖 欢迎大…

C++:仿函数模拟实现STL-priority_queue

优先级队列模拟实现 1.文档了解2.仿函数实现优先级队列仿函数1.定义2.语法3.使用 3.模拟实现源码 1.文档了解 从priority_queue的文档中,我们可以得出以下几点: 1.priority_queue是一个容器适配器 2.priority_queue它实质是一个堆,且默认为大…

【Leetcode】string类刷题

🔥个人主页:Quitecoder 🔥专栏:Leetcode刷题 目录 1.仅反转字母2.字符串中第一个唯一字符3.验证回文串4.字符串相加5.反转字符串I I6.反转字符串中的单词III7.字符串相乘8.把字符串转换为整数 1.仅反转字母 题目链接:…