4

I know Lists make things much easier in Java instead of working with hard-set arrays (lists allow you to add/remove elements at will and they automagically resize, etc).

I've read some stuff suggesting that array's should be avoided in java whenever possible since they are not flexible at all (and sometimes can impose weird limitations, such as if you don't know the size an array needs to be, etc).

Is this "good practice" to stop using arrays at all and use only List logic instead? I'm sure the List type consumes more memory than an array and thus have higher overhead, but is this significant? Most Lists would be GC'ed during runtime if they are left laying around anyways so maybe it isn't as big of a deal as I'm thinking?

SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
  • 3
    It's never significant unless you can prove it's significant. – Lukas Knuth Mar 21 '13 at 17:49
  • [Gorilla or Shark?](http://blog.stackoverflow.com/2011/08/gorilla-vs-shark/) – Robert Harvey Mar 21 '13 at 17:50
  • 1
    If you know the size and are sure that it would not vary you can use arrays. – Sudhanshu Umalkar Mar 21 '13 at 17:50
  • 1
    Its same in all the languages. If you don't know the array size, List is useful otherwise you can do the same thing with Array too. – dejavu Mar 21 '13 at 17:52
  • where do you read this? sounds interesting. – Simulant Mar 21 '13 at 17:54
  • @RobertHarvey I though OP meant using `List` or `ArrayList`, so I marked it as duplicated. My bad. – Luiggi Mendoza Mar 21 '13 at 17:56
  • It's strange, because I've seen similar questions to this one being closed or deleted. – Kakalokia Mar 21 '13 at 17:58
  • This one's closed now also. It just has the new kinder, gentler wording on it. – Robert Harvey Mar 21 '13 at 17:59
  • Thanks everyone for the input. I think I'll start switching to List<> = new ArrayList<>(); for now unless I am certain an array is best. I've been burned before on using arrays and something external happens and caused outofindex error, lists should help prevent that. – SnakeDoc Mar 21 '13 at 18:01
  • 1
    I think the point isn't whether to use `List` or an `Array` but rather to know the differences between the two so to understand, when one is better than the other. Exactly like learning about `pointers` even though you're only coding in `Java` for instance. – Darwind Mar 21 '13 at 18:01
  • @SnakeDoc: That sounds like a logic error. Changing to a list would merely paper over it, unless the approach itself is suspect. – Robert Harvey Mar 21 '13 at 18:02
  • @Darwind I think you're right. for example: String[] strAry = string.split(","); -- here array is best since strAry[] will be set to size of split always. But in other cases, where you must iterate through some data (say, result set) and keep track of some extracted data, without knowing the number of results that will be returned, an Array might be difficult to use here. So a List would be better suited. – SnakeDoc Mar 21 '13 at 18:04
  • 1
    Anytime the number of items is not fixed, or you don't know the number of items, you need a List instead of an Array. That said, if a Data Access Layer is returning an array of items from a query, it will dictate how large the array is anyway, and it's unlikely you would be adding more items to it. – Robert Harvey Mar 21 '13 at 18:08
  • @RobertHarvey with a query like "SELECT orderid, ordervalue FROM orders;" woudl return a result set of unknown length, causing logic difficulties in trying to capture the results in an array, unless you do some poor coding like "String[] str = new String[10000];"... and grossly over-estimate the size of returned data. This is just poor coding in my opinion, and here a List would make more sense. If something returns an array, then its likely in the best data type already and can be used (unless you will be adding new records or removing them and need it to resize). – SnakeDoc Mar 21 '13 at 18:12
  • @SnakeDoc: An array can be iterated with `foreach`, which doesn't care about the size. Arrays get a bad rep; people reach for lists first when they should be reaching for arrays first, and then reaching for a list when they need the additional capabilities that a list provides. – Robert Harvey Mar 21 '13 at 18:16

4 Answers4

9

I don't like dogma. Know the rules; know when to break the rules.

"Never" is too strong, especially when it comes to software.

Arrays and Lists are both potential targets for the GC, so that's a wash.

Yes, you have to know the size of an array before you start. For the cases when you do, there's nothing wrong with it.

It's easy to go back and forth as needed using java.util.Collections and java.util.Arrays classes.

duffymo
  • 305,152
  • 44
  • 369
  • 561
5

I think a good rule of thumb would be to use Lists unless you NEED an Array (for memory/performance reasons). Otherwise Lists are typically easier to maintain and thus less likely to cause future bugs.

Lists provide more flexibility/functionality in terms of auto-expansion, so unless you are either pressed for memory (and can't afford the overhead that Lists create) or do not mind maintaining the Array size as it expands/shrinks, I would recommend Lists.

Try not to micromanage the code too much, and instead focus on more discernible and readable components.

Igor
  • 33,276
  • 14
  • 79
  • 112
2

It depends on the list. A LinkedList probably takes up space only as it's needed, while an ArrayList typically greatly increases its space whenever its capacity is reached. Internally, an ArrayList is implemented using an array, but it's an array that's always larger than what you want. However, since it stores references, not objects, the memory overhead is negligible in most cases and the convenience is worth it, I believe.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
1

I would have to say I follow this approach of using the Collections framework where I might otherwise have used an array. The collections offer you many benefits and convenience over arrays but yes there is likely to be some performance hit.

It is better to write code that is easy to understand and hard to break, arrays require you to put in a lot of checking code to ensure you don't access bits of the array you shouldn't or put to many things in it etc. Given that the majority of the time performance is not a problem it shouldn't be a concern.

Alex Edwards
  • 1,613
  • 3
  • 24
  • 48