A
A
Anton2018-07-07 14:03:19
Java
Anton, 2018-07-07 14:03:19

What does the second brackets do?

How is it different ArrayList<E> name = new ArrayList();from ArrayList<E> name = new ArrayList<>();?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Strannyk, 2018-07-07
@Strangek

ArrayList<String> name = new ArrayList<>();- this is the same as your first line does not make sense, because you are not passing a generic type to the constructor. ArrayList<String> name = new ArrayList<String>();

M
MaxLich, 2018-07-09
@MaxLich

First, if you do not need anything specific ArrayList<T>, then it is better to use type variables List<T>.
Second, try running this code:

ArrayList<String> name = new ArrayList(Arrays.asList(1,2,3));
 for (String s : name) {
      System.out.println(s);
}

And you will immediately understand why you need to specify triangular brackets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question