简体   繁体   中英

What is the difference between list in a list and 2D array?

I am confusing to decide which list has less time complexity since adding items. Does list have less bigO than 2D array? I wanna use it to represent an undirected and unweighted graph with about 1000 vertexes. It is used to store vertexes.

 List<Integer> list

VS.

 int[][] list2 

You would only care if you are talking about huge numbers of elements within your lists/arrays.

The classes provided by collections framework; like ArrayList do add a certain performance price tag; compared to plain old arrays. So, when you have to compute something on 10 million numbers; you might be better of using double[] instead of ArrayList.

But for most other use cases, collections simply provide a much richer interface; this means it is much easier to "optimize" code for readability and maintainability if you avoid using arrays and instead turn to some collection class. And in real world, those properties are actually much more important that the runtime cost of using these more advanced abstraction layers.

And just to be sure to actually answer the question: I think the "O cost" of ArrayList is the same as for arrays; as in big-O of ArrayList would be some "x n"; and array would be "y n"; with x being bigger than y.

So, from a conceptual point, they are within the same class; but from a practical stand point lists are more expensive.

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