using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
myArrayList myArrayList = new myArrayList(new object[] { 1, "hello", true, 'c' });
myArrayList.AddData(2);
myArrayList.InsertData(1, "world");
myArrayList.RemoveData(2);
myArrayList.GetData(0);
// 获取数组的长度和容量
Console.WriteLine("当前元素数量: " + myArrayList.Count); // 输出当前元素数量
Console.WriteLine("数组的容量: " + myArrayList.Capacity); // 输出数组的容量
}
}
public class MyArrayList
{
public object[] obj { get; set; }
public int Count { get { return obj.Length; } }
public int Capacity
{ get
{
int num = obj.length / 8;
if (obj.Length % 8!= 0)
{
num *= 16;
}
return num;
}
}
public MyArrayList(object[] obj)
{
this.obj = obj;
}
public void GetData(int index)
{
object obj1 = obj[index];
if (obj1 is int)
{
int num = (int)obj1;
Console.WriteLine(num);
}
else if (obj1 is string)
{
string str = (string)obj1;
Console.WriteLine(str);
}
else if (obj1 is bool)
{
bool b = (bool)obj1;
Console.WriteLine(b);
}
else if (obj1 is char)
{
char c = (char)obj1;
Console.WriteLine(c);
}
}
public void AddData(object data)
{
object[] newObj = new object[obj.Length + 1];
for (int i = 0; i < obj.Length; i++)
{
newObj[i] = obj[i];
}
newObj[obj.Length] = data;
obj = newObj;
}
public void InsertData(int index, object data)
{
object[] newObj = new object[obj.Length + 1];
for (int i = 0; i < index; i++)
{
newObj[i] = obj[i];
}
newObj[index] = data;
for (int i = index; i < obj.Length; i++)
{
newObj[i + 1] = obj[i];
}
obj = newObj;
}
public void RemoveData(int index)
{
object[] newObj = new object[obj.Length - 1];
for (int i = 0; i < index; i++)
{
newObj[i] = obj[i];
}
for (int i = index + 1; i < obj.Length; i++)
{
newObj[i - 1] = obj[i];
}
obj = newObj;
}
}
}