I am working in Java and would like to know what adding the brackets does. I looked this up and read through the List documentation but couldn't seem to figure it out.
Asked
Active
Viewed 267 times
1
-
3The second is an [array of lists](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). – kiheru May 07 '15 at 07:55
-
1`List
[]` is an array of string lists. the brackets are a language feature of Java and have nothing directly to do with `List`. – Smutje May 07 '15 at 07:55 -
Oh never mind I'm dumb. It must be an array of Lists. – Ayman Elmubark May 07 '15 at 07:55
-
http://stackoverflow.com/questions/26538048/what-do-the-square-brackets-mean-in-java – Kevvvvyp May 07 '15 at 07:56
1 Answers
10
List<String>
is a List
of String
s (i.e. a List
containing String
elements).
List<String>[]
is an array of List
s of String
s (i.e. an array whose elements are List<String>
.
For every type x
, x[]
is an array of elements of type x
.

Eran
- 387,369
- 54
- 702
- 768
-
-
I'd like to ask how is this used. I can declare the array of lists without a problem, however, I cannot seem to initialize it. I receive an error that I cannot create an array of type generic. If that is the case how can an array of lists be used? – Ayman Elmubark May 07 '15 at 08:13
-
@AymanElmubark You can read about it [here](http://stackoverflow.com/questions/217065/cannot-create-an-array-of-linkedlists-in-java). – Eran May 07 '15 at 08:17