简体   繁体   English

列表中的列表和2D数组有什么区别?

[英]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? list的bigO是否少于2D数组? I wanna use it to represent an undirected and unweighted graph with about 1000 vertexes. 我想用它来表示约1000个顶点的无向图和非加权图。 It is used to store vertexes. 它用于存储顶点。

 List<Integer> list

VS. 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; 像ArrayList一样,确实增加了一定的性能价格标签; 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. 您最好使用double []而不是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; 并且只是为了确保确实回答这个问题:我认为ArrayList的“ O成本”与数组相同。 as in big-O of ArrayList would be some "x n"; 因为在ArrayList的big-O中将是“ x n”; and array would be "y n"; 并且数组将为“ y n”; with x being bigger than y. x大于y。

So, from a conceptual point, they are within the same class; 因此,从概念上讲,它们在同一个类中。 but from a practical stand point lists are more expensive. 但从实际的角度来看,清单更昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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