Reputation: 103
What I have is
public static LinkedList<Mp3> musicList;
...
if (musicList == null) {
musicList = new LinkedList<Mp3>();
}//create... this works
but if I have like 5 or more lists how can I do something like this:
Object[] ob = new Object[]{musicList,musicList2,...,musicList10};
for (int i = 0; i < ob.length; i++){
if (ob[i] == null) ob[i] = new LinkedList<Mp3>();
}
If I put it in first way it's working; how can I put it in like in second snippet?
Upvotes: 0
Views: 97
Reputation: 2656
Changing the references in the array will not change the original references used to create the array.
The references in the array are a copy of what was in the initialization.
What you should do is get rid of the musicListN variables and only have an array, or better yet use a List.
List<List<Mp3>> musicLists = new ArrayList<List<Mp3>>(LIST_COUNT);
for (int i = 0; i < LIST_COUNT; i++) {
musicLists.add(new LinkedList<Mp3>());
}
Then use musicLists.get() to everywhere you would have used the older variables.
Upvotes: 2
Reputation: 1183
If you really want to do one line object list initialization, look at this Q. Initialization of an ArrayList in one line
Upvotes: 0
Reputation: 137382
Avoid mixing arrays and generics.
Instead, consider this:
List<List<Mp3>> listsList = new ArrayList<List<Mp3>>();
listsList.add(new LinkedList<Mp3>());
Upvotes: 4