在数组指定索引添加元素的方法

private string[] InsertArr(string[] str, int index, string value)
{
string[] arr = new string[str.Length + 1];
bool b = false;
for (int i = 0; i < str.Length + 1; i++)
{
if (i == index)
{
arr[i] = value;
b = true;
}
else
{
if (b)
{
arr[i] = str[i - 1];
}
else
{
arr[i] = str[i];
}
}
}
return arr;
}

public void aa()
{
int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
Hashtable ht = new Hashtable();
for (int i = 0; i < a.Length; i++)
{
if (ht.ContainsKey(a[i]))
{
ht.Add(a[i], (int)ht[i] + 1);
}
else
{
ht.Add(a[i], 1);
}
}
string str = "";
foreach (DictionaryEntry item in ht)
{
str += item.Key + ":" + item.Value + " ";
}
}

原文地址:https://www.cnblogs.com/Zing/p/4155874.html