5

I am relatively new to Java and over the years I had to solve all sorts of programming problems where the number of data that needs to be collected is really unknown to the programmer.

Is it a good convention to use Lists to collect strings or integer values when the programmer has no way of knowing the amount of variables that needs to be collected? Or is there a better way to handle this using dynamic arrays in Java?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231

5 Answers5

8

If in doubt, using a List is likely to be a better choice even if you know the length.

Using an array can be better for performance, but you need to be an expert to know when this is a good idea, and when it just makes your solution more complicated.

BTW: There is no such thing as dynamic arrays in Java.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    What Java calls an `ArrayList` is commonly known as dynamic array. –  Sep 07 '12 at 15:44
  • What is the difference between a List and an ArrayList? Aren't they both dynamic in a sense? – AnchovyLegend Sep 07 '12 at 15:47
  • @delnan except that's not true. – obataku Sep 07 '12 at 15:52
  • The difference is that when you use List you are making it clear to anyone having the maintain your code that this list could be any list. You should only use ArrayList when it has to be an ArrayList. If you make it something specific when it can be more general, its harder later to determine whether the type can be changed. – Peter Lawrey Sep 07 '12 at 15:55
  • This doesn't suggest it has a common meaning https://www.google.co.uk/search?q=java+"dynamic+array" – Peter Lawrey Sep 07 '12 at 15:56
  • @delnan I did a search for java and "dynamic array" and it didn't suggest to me there was a common meaning. I suspect the term is borrowed from another language which supports dynamic arrays. – Peter Lawrey Sep 07 '12 at 16:02
  • Wikipedia states "C++'s std::vector is an implementation of dynamic arrays, as are the ArrayList[12] classes supplied with the Java API and the .NET Framework." ;) – Peter Lawrey Sep 07 '12 at 16:03
  • @PeterLawrey I never claimed Java guys use the term, I simply don't know if they do. Many other people do use the term though (search for "dynamic array" without mentioning Java) and their definition matches Java's `ArrayList`. –  Sep 07 '12 at 16:03
  • @delnan Wikipedia agrees with you. ;) – Peter Lawrey Sep 07 '12 at 16:11
1

You are doing it right.

List is an Interface and ArrayList is the implementation. If you do not care much for OO mambo-jumbo, here is how you can understand it.

List is the way you want to deal with the "system". You don't care much for anything else.

But the "system" has the freedom to implement it in several ways! ArrayList, LinkedList, Vector etc.

Once you understand this separation, then try to glean the differences and nuances between these implementations.

srini.venigalla
  • 5,137
  • 1
  • 18
  • 29
0

Lists (and their cousins, Map and Set) are a good choice for almost any use case. They eat a little more memory and add a small overhead compared to arrays, but offer a much richer API and generally code written agains an interface is much better reusable/flexible than code using a statically typed array.

One case where the java.util.Collections may add unacceptable overhead is when you want to store primitive data types (such as int, float etc), because to store such types you need to use wrappers (the collection classes only deal with objects). Wrapping can be mostly left to autoboxing, but one needs to be aware of the memory overhead. Apache commons (and possibly other libraries) also offer collection implementation that work with primitives directly.

You only want to work with arrays if you can't avoid it.

Durandal
  • 19,919
  • 4
  • 36
  • 70
0

If your only worry is not knowing how many elements you will need something like a list. By dynamic array, I'm assuming you want to manage your own array and resize it yourself? I wouldn't waste the effort unless you want to learn how it works.

The good thing about Java is that List is an interface with different implementations (like ArrayList, LinkedList, Stack, Vector) depending on your needs.

It looks like ArrayList might work for you.

PPC-Coder
  • 3,522
  • 2
  • 21
  • 30
-1

If you know size of the elements ahead of time and elements are of same type, then arrays are best choice.

Otherwise list are best options to use.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • *"then arrays are best choice."* => not necessarily, even in that case. – assylias Sep 07 '12 at 15:38
  • Not necessarily. `List` has rich functionality since it's part of the Collections API, so I would use it in cases that I know the size ahead of time as well. Not to mention that you shouldn't be mixing types in a `List`. – obataku Sep 07 '12 at 15:39
  • @assylias: Don't forget that lists are backed by arrays. So, always performance overhead. "Rich functionality", I think you can do most of the list operations with arrays also. Hmm applicable in this case or not, is more of debate/expertise issue as Peter said. – kosa Sep 07 '12 at 15:42
  • @Nambari sure you can, but that's not the point. I don't want to implement it all for array when it's already implemented for `List`. – obataku Sep 07 '12 at 15:43
  • 1
    @Nambari Yes there is a performance overhead (which might be negligible depending on what you do with your list/array). However I think using arrays instead of lists everywhere the size is fixed would fall into the "premature optimization" category. I'm not saying arrays have no use. – assylias Sep 07 '12 at 15:43
  • Well I sort of agree with your points, it is more related to "What you do with it" and there wouldn't be any end for this debate. – kosa Sep 07 '12 at 15:49