如何计算1D阵列如果我无法预测它的长度?

时间:2021-07-11 01:48:42

Duplicate

Array of Unknown Length in C#

C#中未知长度的数组

How do I initialize string[] without a need of initializing the length? I want it to be dynamic array, so when I add something, Length increases and no Exception raises?

如何在不需要初始化长度的情况下初始化string []?我希望它是动态数组,所以当我添加一些东西时,长度增加并且没有异常提升?

Should I just use some kind of List?

我应该使用某种List吗?

7 个解决方案

#1


If you need to have an array of unkown length of strings, use a List<T> from the System.Collections.Generic namespace.

如果需要一个未知长度的字符串数组,请使用System.Collections.Generic命名空间中的List

List<string> myList = new List<string>();
myList.Add("First");
myList.Add("Second");
myList.Add("Third");
myList.Count // = 3

This uses an array behind the scenes that is of a fixed size, but it hides the fact that it will make the array bigger and move all elements when it runs out of space.

这使用了具有固定大小的幕后数组,但它隐藏了这样一个事实:它会使数组更大并在空间用完时移动所有元素。

#2


List<T> will do what you want. This has a ToArray() method that will return an array when you're done adding items (if you still need an array).

List 会做你想要的。这有一个ToArray()方法,当你完成添加项目时,它将返回一个数组(如果你仍然需要一个数组)。

#3


I think a lot of languages out there will allocate the entire array as a block up front. You may need to use a wrapper class that provides this functionality. In the absence of a language specified, Java and C++ for example, int[] will be a contiguous block of memory. You'll have to allocate a new block and copy the old contents, or use a wrapper that manages this for you, either doing the same thing or using pointers to objects. That would be ArrayList or similar in Java.

我认为很多语言会将整个阵列分配为一个块。您可能需要使用提供此功能的包装类。如果没有指定语言,例如Java和C ++,int []将是一个连续的内存块。您必须分配一个新块并复制旧内容,或者使用一个为您管理此内容的包装器,或者执行相同操作或使用指向对象的指针。这将是Java中的ArrayList或类似的东西。

#4


What language?

C#, Java, and many other high-level languages have ArrayList data structure (or something similar) that allow you to add and remove elements without setting a specific size.

C#,Java和许多其他高级语言都具有ArrayList数据结构(或类似的东西),允许您在不设置特定大小的情况下添加和删除元素。

#5


Well, in fortran you could use ALLOCATE.

好吧,在fortran你可以使用ALLOCATE。

#6


Arrays have a fixed length in most languages. You have to use a dynamic datatype like a list if you need dynamic length.

在大多数语言中,数组具有固定长度。如果需要动态长度,则必须使用类似于列表的动态数据类型。

If the language does not suport lists natively, they are usually implemented with arrays. If the content becomes to long to fit into the array, a longer array is allocated, the content copied from the old array to the new one, and finally the old array is freed.

如果语言本身不支持列表,则通常使用数组实现。如果内容变得很长以适合数组,则分配更长的数组,将内容从旧数组复制到新数组,最后释放旧数组。

I am quite sure there is a dynamic length type in your language or one of its libraries.

我很确定您的语言或其库中有一个动态长度类型。

UPDATE

For C# the collection classes are found in the System.Collections namespace. You should focus on the generic classes in System.Collections.Generic if possible. Four your case the generic List<String> would probably fit best. But also Stack<String> or Queue<String> might be interesting.

对于C#,集合类可在System.Collections命名空间中找到。如果可能,您应该专注于System.Collections.Generic中的泛型类。四种情况下,通用List 可能最适合。但是Stack 或Queue 可能也很有趣。

#7


If your using C# use ArrayList. It dynamically expands as you need it and it also can hold anything (this can be both good and bad). So yes you should use some kind of list. Also remember to include this using statement:

如果您使用C#使用ArrayList。它可以根据您的需要动态扩展,也可以保存任何东西(这可能既好又坏)。所以是的,你应该使用某种列表。还记得包含这个using语句:

using System.Collections;

#1


If you need to have an array of unkown length of strings, use a List<T> from the System.Collections.Generic namespace.

如果需要一个未知长度的字符串数组,请使用System.Collections.Generic命名空间中的List

List<string> myList = new List<string>();
myList.Add("First");
myList.Add("Second");
myList.Add("Third");
myList.Count // = 3

This uses an array behind the scenes that is of a fixed size, but it hides the fact that it will make the array bigger and move all elements when it runs out of space.

这使用了具有固定大小的幕后数组,但它隐藏了这样一个事实:它会使数组更大并在空间用完时移动所有元素。

#2


List<T> will do what you want. This has a ToArray() method that will return an array when you're done adding items (if you still need an array).

List 会做你想要的。这有一个ToArray()方法,当你完成添加项目时,它将返回一个数组(如果你仍然需要一个数组)。

#3


I think a lot of languages out there will allocate the entire array as a block up front. You may need to use a wrapper class that provides this functionality. In the absence of a language specified, Java and C++ for example, int[] will be a contiguous block of memory. You'll have to allocate a new block and copy the old contents, or use a wrapper that manages this for you, either doing the same thing or using pointers to objects. That would be ArrayList or similar in Java.

我认为很多语言会将整个阵列分配为一个块。您可能需要使用提供此功能的包装类。如果没有指定语言,例如Java和C ++,int []将是一个连续的内存块。您必须分配一个新块并复制旧内容,或者使用一个为您管理此内容的包装器,或者执行相同操作或使用指向对象的指针。这将是Java中的ArrayList或类似的东西。

#4


What language?

C#, Java, and many other high-level languages have ArrayList data structure (or something similar) that allow you to add and remove elements without setting a specific size.

C#,Java和许多其他高级语言都具有ArrayList数据结构(或类似的东西),允许您在不设置特定大小的情况下添加和删除元素。

#5


Well, in fortran you could use ALLOCATE.

好吧,在fortran你可以使用ALLOCATE。

#6


Arrays have a fixed length in most languages. You have to use a dynamic datatype like a list if you need dynamic length.

在大多数语言中,数组具有固定长度。如果需要动态长度,则必须使用类似于列表的动态数据类型。

If the language does not suport lists natively, they are usually implemented with arrays. If the content becomes to long to fit into the array, a longer array is allocated, the content copied from the old array to the new one, and finally the old array is freed.

如果语言本身不支持列表,则通常使用数组实现。如果内容变得很长以适合数组,则分配更长的数组,将内容从旧数组复制到新数组,最后释放旧数组。

I am quite sure there is a dynamic length type in your language or one of its libraries.

我很确定您的语言或其库中有一个动态长度类型。

UPDATE

For C# the collection classes are found in the System.Collections namespace. You should focus on the generic classes in System.Collections.Generic if possible. Four your case the generic List<String> would probably fit best. But also Stack<String> or Queue<String> might be interesting.

对于C#,集合类可在System.Collections命名空间中找到。如果可能,您应该专注于System.Collections.Generic中的泛型类。四种情况下,通用List 可能最适合。但是Stack 或Queue 可能也很有趣。

#7


If your using C# use ArrayList. It dynamically expands as you need it and it also can hold anything (this can be both good and bad). So yes you should use some kind of list. Also remember to include this using statement:

如果您使用C#使用ArrayList。它可以根据您的需要动态扩展,也可以保存任何东西(这可能既好又坏)。所以是的,你应该使用某种列表。还记得包含这个using语句:

using System.Collections;