1
ArrayList<Integer>[] paths = new ArrayList[N];

This line is part of a code given to me in an Assignment to use as part of Djikstra's algorithm. Since it was given to me in the assignment I assume it's syntactically correct but I don't think I've ever seen an ArrayList being declared with the brackets used to declare Arrays.

Is this an ArrayList or an Array? What do the brackets mean in this case?

Ondra K.
  • 2,767
  • 4
  • 23
  • 39
pog
  • 19
  • 1
  • 1
    seems to be an array of arraylists – Stultuske Feb 22 '21 at 08:33
  • I recommend *not* to mix arrays and `ArrayList`s. – MC Emperor Feb 22 '21 at 08:34
  • 2
    Why not mix Arrays and ArrayLists? – testo Feb 22 '21 at 08:35
  • 1
    @testo Because arrays have limited flexibility, and since you're already using a list (imposing a small overhead), so there is no need to also use an array. [More about the subject](https://stackoverflow.com/questions/2391553/why-is-it-preferred-to-use-lists-instead-of-arrays-in-java). Also, arrays don't work well with generics. E.g. `new ArrayList` is valid, but `new T[0]` isn't. – MC Emperor Feb 22 '21 at 08:46
  • I would say a reason to not mix them is that it's hard to read and understand quickly... `Integer[][]` is easy to read and `ArrayList>` is easy to read, but that is just confusing at first glance. – Phillip Feb 22 '21 at 08:50

2 Answers2

5

An array of ArrayList-Instances.

testo
  • 1,052
  • 2
  • 8
  • 24
3

to be very exact (or even pedantic), it is an array with N elements:
= new ArrayList[N].

Each element itself can be an ArrayList - but after that statement, the array is just filled with nulls.