C#基础加强_ArrayList基本原理

news/2024/7/5 4:44:23

 小弟初写博文,深感“易敲千行码,难下百余文”的道理。

内容粗略浅薄,望各位大神海涵!


  •  数组是包含若干相同类型变量,这些变量都可以通过索引进行访问。它在内存中是一串连续的空间,所以长度一旦确定就不能再修改。
       //声明一个长度为6的数组
       int [] arr = new int [6];
  • 动态数组是包含若干object类型变量,变量也可以通过索引访问。但是它的长度可以动态增长,可以通过Add()方法添加元素,Remove()方法删除元素,通过Count属性遍历元素,等等。
        //新建一个动态数组
            ArrayList arr = new ArrayList();
            arr.Add("1");       //加元素
            arr.Add("2");
            arr.Add("3");
            arr.Add("4");
            arr.Add("5");
            arr.RemoveAt(4);  //删元素
            //for遍历
            for (int i = 0; i < arr.Count; i++)  
            {
                Console.WriteLine(arr[i]);
            }
            Console.ReadKey();
  • 可以新建一个类,包含一个string类型数组,封装一些属性和方法来实现类似ArrayList的功能。
class MyArrayList
    {
        


     //创建一个长度为4的数组
     string
[] strs=new string[4]; int count; /// <summary> /// 元素个数 /// </summary> public int Count { get { return count; } //set { count = value; } } /// <summary> /// 添加元素 /// </summary> /// <param name="item"></param> public void Add(string item) { //判断数组是否需要扩容 if(count==strs.Length) { //数组需要重新创建,创建一个长度为原数组的2倍的数组
string [] newstrs=new string[strs.Length*2]; //将原始数组的数据复制到新数组中 strs.CopyTo(newstrs, 0); //将旧数组的引用重新指定 strs = newstrs; } strs[count] = item; //存储元素 count++; //元素个数加1
} /// <summary> /// 删除元素 /// </summary> /// <param name="index">需要删除的索引</param> public void RemoveAt(int index) { Array.Copy(strs, index + 1, strs, index, this.Count - 1 - index);
       strs[count-1] = null;
       this.count--;
} /// <summary> /// 索引器 /// </summary> /// <param name="index"></param> /// <returns></returns> public string this[int index] { get { if (index < 0 || index >= this.Count) { throw new ArgumentOutOfRangeException("索引越界了啦"); } return strs[index]; } set { if (index < 0 || index >= this.Count) { throw new ArgumentOutOfRangeException("索引越界了啦"); } strs[index] = value; } }
  • 创建一个MyArrayList的string集合,可以实现ArrayList的功能
            //新建一个MyArrayList集合
            MyArrayList my = new MyArrayList();
            my.Add("1");      //添加元素
            my.Add("2");
            my.Add("3");
            my.Add("4");
            my.Add("5");
            my.RemoveAt(4);   //删除元素
            //for遍历
            for (int i = 0; i < my.Count; i++)
            {
                Console.WriteLine(my[i]);
            }
            Console.ReadKey();
  • ArrayList动态数组没有指定类型,它包含的是object类数组,那么我们只要把MyArrayList类包含的string [] 改成object[],就更类似ArrayList,当然ArrayList还包含很多其他的功能方法,这里就不一一描述了。

 

 

转载于:https://www.cnblogs.com/lant-li/p/3827278.html


http://www.niftyadmin.cn/n/4137515.html

相关文章

LeetCode300. 最长递增子序列【动态规划】

难度&#xff1a;中等 题目描述&#xff1a; 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元素的顺序。例如&#xff0c;[3,6,2,7] 是数组[…

Day2 数据类型、数据运算、数据字典

1.数字&#xff1a;int&#xff08;整型&#xff09; 32位机器&#xff1a;-2**31~2**31-1 64位机器&#xff1a;-2**63~2**63-1 float&#xff08;浮点型&#xff09; 2.布尔值 真或假 1或0 bool(0) 3.字符串 name “wanghuafeng” print(“my name is ” name “and you?”…

用Lucene实现分组,facet功能,FieldCache

假如你像用lucene来作分组&#xff0c;比如按类别分组&#xff0c;这种功能&#xff0c;好了你压力大了&#xff0c;lucene本身是不支持分组的。 当你想要这个功能的时候&#xff0c;就可能会用到基于lucene的搜索引擎solr。 不过也可以通过编码通过FieldCache和单字段&#xf…

I.MX6 wpa_supplicant_8 编译问题

/************************************************************************* I.MX6 wpa_supplicant_8 编译问题* 说明&#xff1a;* 在移植wifi过程中&#xff0c;要编译wpa_supplicant_8这个模块&#xff0c;记录一下问题。** …

LeetCode206. 反转链表

难度&#xff1a;简单 题目描述&#xff1a; 给你单链表的头节点 head &#xff0c;请你反转链表&#xff0c;并返回反转后的链表。 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr)…

SQL Server中一个隐性的IO性能杀手-Forwarded record

SQL Server中一个隐性的IO性能杀手-Forwarded record 原文:SQL Server中一个隐性的IO性能杀手-Forwarded record简介 最近在一个客户那里注意到一个计数器很高&#xff08;Forwarded Records/Sec&#xff09;&#xff0c;伴随着间歇性的磁盘等待队列的波动。本篇文章分享什么是…

reactjs学习笔记2--组件的介绍

什么是组件 组件就像是乐高积木&#xff0c;一个完整的房子&#xff0c;可以由砖头一块一块的组成&#xff0c;一块砖头&#xff0c;就可以称为一个组件。 如何创建组件 调用 React.createClass 方法&#xff0c;传入的参数为一个对象&#xff0c;对象必须定义一个 render 方法…

textarea焦点的用法(转)

1.文本框显示默认文字&#xff1a; <textarea>白鸽男孩</textarea> <textarea>白鸽男孩</textarea>   2.鼠标点击文本框&#xff0c;默认文字消失&#xff1a; <textarea οnfοcus”if(value’白鸽男孩’) {value’ ‘}”>白鸽男孩&…