通过实例学C#之ArrayList

介绍

        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.mfbz.cn/a/557970.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

使用go和消息队列优化投票功能

文章目录 1、优化方案与主要实现代码1.1、原系统的技术架构1.2、新系统的技术架构1.3、查看和投票接口实现1.4、数据入库MySQL协程实现1.5、路由配置1.6、启动程序入口实现 2、压测结果2.1、设置Jmeter线程组2.2、Jmeter聚合报告结果,支持11240/秒吞吐量2.3、Jmeter…

vue 一键更换主题颜色

这里提供简单的实现步骤,具体看自己怎么加到项目中 我展示的是vue2 vue3同理 在 App.vue 添加 入口处直接修改 #app { // 定义的全局修改颜色变量--themeColor:#008cff; } // 组件某些背景颜色需要跟着一起改变,其他也是同理 /deep/ .ant-btn-primar…

『FPGA通信接口』汇总目录

Welcome 大家好,欢迎来到瑾芳玉洁的博客! 😑励志开源分享诗和代码,三餐却无汤,顿顿都被噎。 😭有幸结识那个值得被认真、被珍惜、被捧在手掌心的女孩,不出意外被敷衍、被唾弃、被埋在了垃圾堆。…

【Linux学习】Linux编辑器-vim使用

这里写目录标题 1. 🌠vim的基本概念🌠2. vim的基本操作🌠3.vim异常处理🌠4. vim正常模式的相关命令🌠5. vim末(底)行模式相关命令 vi/vim都是多模式编辑器,不同的是vim是vi的升级版本…

开发与产品的战争之自动播放视频

开发与产品的战争之自动播放视频 起因 产品提了个需求,对于网站上的宣传视频,进入页面就自动播放。但是基于我对chromium内核的一些浅薄了解,我当时就给拒绝了: “浏览器不允许”。(后续我们浏览器默认都是chromium内核的&#…

2024年华中杯数模竞赛A题完整解析(附代码)

2024年华中杯数模竞赛A题 基于动态优化的太阳能路灯光伏板朝向以最大化能量收集研究摘要问题重述问题分析模型假设符号说明 代码问题一 完整资料获取 基于动态优化的太阳能路灯光伏板朝向以最大化能量收集研究 摘要 随着可再生能源技术的发展,太阳能作为一种清洁的…

C++类与对象(中)②

目录 1.赋值运算符重载 1.1运算符重载 1.2赋值运算符重载 1.2.1赋值运算符重载格式 1.2.2赋值运算符只能重载成成员函数不能重载成全局函数 1.2.3同拷贝函数一样,如果类是形如日期类这样变量全是内置类型的,赋值运算符就必须自己实现,…

Spectre-v1 简介以及对应解决措施

文章目录 前言一、Variant 1: Exploiting Conditional Branches.二、 BACKGROUND2.1 Out-of-order Execution2.2 Speculative Execution2.3 Branch Prediction2.4 The Memory Hierarchy2.5 Microarchitectural Side-Channel Attacks2.6 Return-Oriented Programming 三、 ATTAC…

大学生简历大赛演讲稿(6篇)

大学生简历大赛演讲稿(6篇) 以下是六篇大学生简历大赛演讲稿的范文,供您参考: 范文一:展现真我,点亮未来 尊敬的评委、亲爱的同学们: 大家好! 今天,我站在这里&#xf…

区块链实验室(35) - 编译solana for ARM64版

今天终于成功编译solana for arm64版,编译时间巨长。见下图所示。编译步骤详见solana网站https://github.com/solana-labs/solana和https://docs.solanalabs.com/。

【C语言】【数据结构】项目实践——贪吃蛇游戏(超详细)

前言 本篇博客我们来实现一个小游戏项目——贪吃蛇,相信肯定很多人都玩过,那么整个贪吃蛇是怎么实现出来的那,这个项目用到了很多方面的知识:C语言函数、枚举、结构体、动态内存管理、预处理指令、链表、Win32 API等。我们就通过这…

nodejs工具模块学习

util 是一个Node.js 核心模块,提供常用函数的集合; util.inspect(object,[showHidden],[depth],[colors]) 是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出; 如果只有一个参数 object,是要转换的对象&…

网工内推 | 兴业银行总行正编,科技运维部,硕士以上学历

01 兴业银行 招聘岗位:安全渗透专家 职责描述: 1.负责牵头组织本行红蓝对抗、攻防演练等工作; 2.负责牵头制定有效的渗透测试方案,开展对本行防御体系的验证工作; 3.负责牵头组织本行各类应用系统的渗透测试与漏洞扫…

java的Spring XML和注解解析深入理解

正文 熟悉IOC体系结构 要学习Spring源码,我们首先得要找准入口,那这个入口怎么找呢?我们不妨先思考一下,在Spring项目启动时,Spring做了哪些事情。这里我以最原始的xml配置方式来分析,那么在项目启动时&a…

大型网站系统架构演化实例_5.使用反向代理和CDN加速网站响应

1.使用反向代理和CDN加速网站响应 随着网站业务不断发展,用户规模越来越大,由于区域的差别使得网络环境异常复杂,不同地区的用户访问网站时,速度差别也极大。有研究表明,网站访问延迟和用户流失率正相关,网…

二叉检索树(定义、意义、存储数据元素形式),二叉检索树插入方法的图解和实现

1、二叉检索树: (1)定义 二叉检索树的任意一个结点,设其值为k,则该节点左子树中任意一个结点的值都小于k;该节点右子树中任意一个节点的值都大于或等于k 这里的比较规则可以是针对数字的,也可…

js实现抽奖效果

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>随机抽奖</title> </head> <body>…

synchronized锁升级原理

锁升级过程 jdk1.6之后的优化 synchronized锁有四种状态&#xff0c;无锁&#xff0c;偏向锁&#xff0c;轻量级锁&#xff0c;重量级锁&#xff0c;这几个状态会随着竞争状态逐渐升级&#xff0c;锁可以升级但不能降级&#xff0c;但是偏向锁状态可以被重置为无锁状态。 1、偏…

C++ 类和对象(终篇)

初始化列表 就是给我们每一个成员变量找了一个定义的位置&#xff0c;不然像const这样的成员不好处理 所有的成员能在初始化列表初始化的都在里面初始化 拷贝构造函数和构造函数都允许初始化 构造函数体中的语句只能将其称作为赋初值&#xff0c;而不能称作初始化。 因为初始…

牛客NC314 体育课测验(一)【中等 图,BFS,拓扑排序 Java,Go、PHP】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/1a16c1b2d2674e1fb62ce8439e867f33 核心 图&#xff0c;BFS,拓扑排序&#xff0c;队列参考答案Java import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修…
最新文章