我想以編程方式向C#中的字符串數組添加或刪除一些元素,但仍然保留之前的項目,有點像VB函數ReDim Preserve。向C#數組添加元素
回答
明顯的建議是使用List<string>
,這樣你們會從其他的答案已經閱讀。這絕對是真實開發場景中的最佳方式。
當然,我想讓事情變得更有趣(我的那一天),所以我會直接回答你的問題。
這裏有一對夫婦的功能,將新增和string[]
刪除元素...
string[] Add(string[] array, string newValue){
int newLength = array.Length + 1;
string[] result = new string[newLength];
for(int i = 0; i < array.Length; i++)
result[i] = array[i];
result[newLength -1] = newValue;
return result;
}
string[] RemoveAt(string[] array, int index){
int newLength = array.Length - 1;
if(newLength < 1)
{
return array;//probably want to do some better logic for removing the last element
}
//this would also be a good time to check for "index out of bounds" and throw an exception or handle some other way
string[] result = new string[newLength];
int newCounter = 0;
for(int i = 0; i < array.Length; i++)
{
if(i == index)//it is assumed at this point i will match index once only
{
continue;
}
result[newCounter] = array[i];
newCounter++;
}
return result;
}
感謝大家花時間回答我的問題和回答!你們每個人都對我有用!特別感謝你,你的回答是我正在尋找的那個! – Cippo 2012-03-05 17:37:29
不要使用數組 - 使用允許動態添加項的通用List<T>
。
如果這不是一個選項,您可以使用Array.Copy
或Array.CopyTo
將數組複製到一個更大的數組中。
使用List<string>
而不是string[]
。
列表允許您添加和刪除具有良好性能的項目。
因爲數組實現IEnumerable<T>
您可以使用Concat
:
string[] strArr = { "foo", "bar" };
strArr = strArr.Concat(new string[] { "something", "new" });
還是什麼會更合適。將使用支持內嵌操縱集合類型。
你應該看看List
object。列表往往更好地動態變化,就像你想要的一樣。數組沒有這麼多...
可以使用泛型集合,如清單<>
List<string> list = new List<string>();
// add
list.Add("element");
// remove
list.Remove("element");
從數組中刪除元素的等價概念可能通過'RemoveAt'方法,因爲它需要索引值。 'Remove'刪除了第一個可能需要的事件,但似乎更難控制。 – Brad 2012-03-05 17:57:11
如果你真的不(或不能)使用的,而不是你的陣列泛型集合, Array.Resize是C#的版本REDIM的保護:
var oldA = new [] {1,2,3,4};
Array.Resize(ref oldA,10);
foreach(var i in oldA) Console.WriteLine(i); //1 2 3 4 0 0 0 0 0 0
什麼abaut這一個:
List<int> tmpList = intArry.ToList(); tmpList.Add(anyInt); intArry = tmpList.ToArray();
您可以使用此代碼片段:
static void Main(string[] args)
{
Console.WriteLine("Enter number:");
int fnum = 0;
bool chek = Int32.TryParse(Console.ReadLine(),out fnum);
Console.WriteLine("Enter number:");
int snum = 0;
chek = Int32.TryParse(Console.ReadLine(),out snum);
Console.WriteLine("Enter number:");
int thnum = 0;
chek = Int32.TryParse(Console.ReadLine(),out thnum);
int[] arr = AddToArr(fnum,snum,thnum);
IOrderedEnumerable<int> oarr = arr.OrderBy(delegate(int s)
{
return s;
});
Console.WriteLine("Here your result:");
oarr.ToList().FindAll(delegate(int num) {
Console.WriteLine(num);
return num > 0;
});
}
public static int[] AddToArr(params int[] arr) {
return arr;
}
我希望這會幫助到你,只是改變類型
- 1. 向C#數組添加一個元素
- 2. 從C++中的指針數組向元素添加元素
- 3. 向多維數組添加元素
- 4. 向android中的數組添加元素
- 5. 向PHP關聯數組添加元素
- 6. 在Perl中向數組添加元素
- 7. 在C++中向數組結構添加元素
- 8. 添加數組元素
- 9. 添加數組元素的
- 10. 添加數組元素Javascript
- 11. 向現有的javascript數組添加數組元素
- 12. 向量的結構:添加元素C++
- 13. 向其他元素添加元素
- 14. 向數組中的每個元素/整數添加數字
- 15. C++中的數組 - 添加數組的元素
- 16. jquery,向元素添加類
- 17. 向canvas添加元素html5
- 18. 向WPF Grid添加元素
- 19. 從mysql數據庫中每天向數組中添加元素
- 20. C++將數組元素向右推
- 21. 整數數組列表添加元素
- 22. 在Zend_Form_Element_Select中向選項數組添加額外元素
- 23. 如何在Android上向字符串數組添加元素
- 24. 如果我向數組中添加1個元素,WCF超時
- 25. 使用jq向現有JSON數組添加新元素
- 26. PHP使用foreach()向數組添加元素
- 27. 通過索引向數組添加元素,增量?
- 28. 向perl中的數組中的所有元素添加引號
- 29. simplecarousel:向分頁添加數組元素ul
- 30. 在foreach循環中向多維數組添加元素(PHP)
你嘗試過什麼?由於它的基本性質,這個問題在3分鐘內可能會有50個答案。谷歌會一樣有幫助。 – 2012-03-05 17:17:45
vb的redim保存是和邪惡的。你總是**,毫無例外,最好使用集合類型。而且我通常不會那樣做絕對的聲明。 – 2012-03-05 17:19:33
減號5似乎對這個問題有點苛刻。我從這個概念和從VBA轉移到C#的過渡中掙扎。 – Brad 2012-03-05 17:51:30