Java: Should I always replace arrays for ArrayLists?

Well, it seems to me that ArrayLists simplify code expansion later because they can grow and because they simplify the use of Generics. However, for multidimensional arrays, I find that code readability is better with standard arrays.

In any case, are there any recommendations for using this or that? For example, I'm going to return a table from a function ( int[][] ), but I was wondering if it would be better to return List<List<Integer>> or List<int[]> .

+4
source share
5 answers

Unless you have a good reason, I would recommend using lists by arrays.

There are some specific cases when you want to use an array (for example, when you implement your own data structures or when you address a very specific performance requirement that you have profiled and identified as a bottleneck), but for general purposes, Lists are more convenient and will be offer you more flexibility in how you use them.

If you are able, I would also recommend programming an abstraction (List), rather than a specific type (ArrayList). Again, this gives you flexibility if you decide to describe in detail the implementation details in the future.

To eliminate your readability point: if you have a complex structure (e.g. ArrayList from HashMaps of ArrayLists), consider either encapsulating this complexity in a class, or / or creating some very clearly named functions to manage the structure.

+7
source

Select a data structure implementation and interface based on core usage:

  • Random access: use List for variable type and ArrayList under hood

  • Addendum: use Collection for variable type and LinkedList under the hood

  • Loop and process: use Iterable and see above for use in the hood based on the manufacturer code

Use the most abstract interface when transferring data. Also, do not use Collection when you need random access. List has get (int) , which is very useful when random access is needed.

Typed collections such as List <String> make up the syntactic convenience of arrays.

Do not use arrays unless you have a qualified performance evaluator . Even then you should get a second opinion. Arrays are usually premature optimizations and should be avoided.

Generally speaking, you are much better off using an interface rather than a specific type. A particular type makes it difficult to process the internal parts of the function in question. For example, if you return int [] [], you must do all the calculations in advance. If you return List>, you can lazily perform calculations during iteration (or even simultaneously in the background), if useful.

+3
source

List more powerful:

  • You can resize the list after creating it.
  • You can create read-only data views.
  • It can be easily combined with other collections such as Set or Map .

The array works at a lower level:

  • Its contents can always be changed.
  • Its length can never be changed.
  • It uses less memory.
  • You may have arrays of primitive data types.
+1
source

I would like to point out that lists can contain wrappers for primitive data types that would otherwise have to be stored in an array. (i.e., the Double class, which has only one field: double). Newer versions of Java are converted to and from these shells implicitly, at least in most cases, so the possibility of placing primitives in your lists should not be considered for the vast majority of cases.

For completeness: the only time I saw that Java was implicitly converted from a primitive shell was that these wrappers were composed in a higher order structure: it could not convert Double [] to double [].

+1
source

It basically comes down to flexibility / ease of use and efficiency. If you do not know how many elements will be required in advance, or if you need to insert a middle, it is better to choose ArrayLists. I suppose they use arrays under the hood, so you need to think about using the ensureCapacity method for performance. Arrays are preferable if you have a fixed size in advance and you don't need inserts, etc.

0
source

Source: https://habr.com/ru/post/1315996/


All Articles