简体   繁体   中英

C# arrays with unknown size

I need class with array inside:

class FirstClass
{
     int x = 0;
     int y = 0;
}

class SecondClass
{
    FirstClass[] firstclass = ???????? // size of array is unknown
    OtherClass[][] otherclass = ???????? // i need also multidimensional array or array of arrays
}

Normally im using List<> for purposes like this, but today i need fastest possible solution (a lot of data to process), and i think Array should be faster than List (I've read somewhere that List is a lot slower than Array).

Question is - what to put into "????????" in my code?

Edit:

My class is a neural network object. Biggest array will contain learning data (100000+ data rows with 50+ fields, float or double datatype).

If Array is 1% faster than List - i need Array.

You cannot make an array until you know its exact size: unlike lists, arrays cannot grow * ; once you give them a size, they keep that size forever. If you must have an array for processing, make a List<T> first, populate it, and then convert to an array with ToArray() method:

var firstClassList = new List<FirstClass>();
firtstClassList.Add(new FirstClass(123));
firtstClassList.Add(new FirstClass(456));
FirstClass firstClass[] = firtstClassList.ToArray();

var otherClassList = new List<List<OtherClass>>();
otherClassList.Add(new List<OtherClass>());
otherClassList[0].Add(OtherClass(10));
otherClassList[0].Add(OtherClass(11));
otherClassList.Add(new List<OtherClass>());
otherClassList[1].Add(OtherClass(20));
otherClassList[1].Add(OtherClass(21));
otherClassList[1].Add(OtherClass(22));
OtherClass otherClass[][] = otherClassList.Select(r => r.ToArray()).ToArray();

Note, however, that this is premature optimization: lists are very much as fast as arrays, but they offer a lot more flexibility. Replacing lists with arrays does not make sense, at least not before you profile.


* Calls of Array.Resize to change the size of the array do not count as "growing" the array, because you end up with a different array instance, and the one you were trying to "grow" gets thrown away immediately after being copied.

If you are unsure of the size of the Arrays, you may want to consider using a different type of collection, such as a List<FirstClass> as you mentioned or an ArrayList etc.

Performance-wise, until you have actually tested the data, you may be fine using a List and if you still have issues or bottlenecks, consider using an Array or some more efficient structure. (The run times shouldn't differ too greatly unless you are working with very large sets of data.)

(There is always the option to convert your lists to arrays as well.)

Why would Array be faster than a List? See this question for a comparison of the running times.

The only case I can think of where an Array would beat a list is if you needed to search for an element, and the array was sorted so you could do a binary search. However, since you can use [] to index List<T>s in C#, that's a moot point.

var list = new List<FirstClass>();
list.Add(object);

FirstClass[] firstClass = list.ToArray();

var listOfLists = new List<List<SecondClass>>();
var item1 = new List<SecondClass>();
item1.Add(anotherObject);
listOfLists.Add(item1);

SecondClass[][] secondClass = listsOfLists.Select(i => i.ToArray()).ToArray();

You can just instantiate them like this:

FirstClass[] firstclass = new FirstClass[];
OtherClass[][] otherclass = new OtherClass[][];

And then just use the Add method whenever you need to add an element:

firstclass.Add(new FirstClass());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM