将元素添加到 C# 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9570944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding elements to a C# array
提问by Cippo
I would like to programmatically add or remove some elements to a string array in C#, but still keeping the items I had before, a bit like the VB function ReDim Preserve.
我想以编程方式向 C# 中的字符串数组添加或删除一些元素,但仍保留我以前拥有的项目,有点像 VB 函数ReDim Preserve。
采纳答案by musefan
The obvious suggestion would be to use a List<string>instead, which you will have already read from the other answers. This is definitely the best way in a real development scenario.
显而易见的建议是使用 aList<string>代替,您已经从其他答案中阅读了它。这绝对是实际开发场景中最好的方式。
Of course, I want to make things more interesting (my day that is), so I will answer your question directly.
当然,我想让事情变得更有趣(那是我的一天),所以我会直接回答你的问题。
Here are a couple of functions that will Add and Remove elements from a 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;
}
回答by Oded
Don't use an array - use a generic List<T>which allows you to add items dynamically.
不要使用数组 - 使用List<T>允许您动态添加项目的泛型。
If this is not an option, you can use Array.Copyor Array.CopyToto copy the array into a larger array.
如果这不是一个选项,您可以使用Array.Copy或Array.CopyTo将数组复制到更大的数组中。
回答by Kendall Frey
Use List<string>instead of string[].
使用List<string>代替string[]。
List allows you to add and remove items with good performance.
List 允许您添加和删除具有良好性能的项目。
回答by M.Babcock
Since arrays implement IEnumerable<T>you can use Concat:
由于数组实现,IEnumerable<T>您可以使用Concat:
string[] strArr = { "foo", "bar" };
strArr = strArr.Concat(new string[] { "something", "new" });
Or what would be more appropriate would be to use a collection type that supports inline manipulation.
或者更合适的是使用支持内联操作的集合类型。
回答by Brad
回答by auraham
You can use a generic collection, like List<>
您可以使用通用集合,例如 List<>
List<string> list = new List<string>();
// add
list.Add("element");
// remove
list.Remove("element");
回答by Paolo Falabella
If you really won't (or can't) use a generic collection instead of your array, Array.Resizeis c#'s version of redim preserve:
如果您真的不会(或不能)使用泛型集合而不是数组,则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
回答by René Carannante
What's abaut this one:
这是怎么回事:
List<int> tmpList = intArry.ToList();
tmpList.Add(anyInt);
intArry = tmpList.ToArray();
List<int> tmpList = intArry.ToList();
tmpList.Add(anyInt);
intArry = tmpList.ToArray();
回答by Kirill Shur
You can use this snippet:
您可以使用此代码段:
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;
}
I hope this will help to you, just change the type
我希望这对你有帮助,只是改变类型

