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?

5
  • 1
    seems to be an array of arraylists Commented Feb 22, 2021 at 8:33
  • I recommend not to mix arrays and ArrayLists. Commented Feb 22, 2021 at 8:34
  • 2
    Why not mix Arrays and ArrayLists? Commented Feb 22, 2021 at 8: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. Also, arrays don't work well with generics. E.g. new ArrayList<T> is valid, but new T[0] isn't. Commented Feb 22, 2021 at 8: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<ArrayList<Integer>> is easy to read, but that is just confusing at first glance. Commented Feb 22, 2021 at 8:50

2 Answers 2

5

An array of ArrayList-Instances.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.