0

Have a question.. I have a list of mp3 filenames to add a new file name use:

musicList.add(new Mp3(id, filename));

now I wanted to create a array containing this lists

 private static final int LIST_COUNT = 8;
 public static List<List<Mp3>> musicLists = new ArrayList<List<Mp3>>(LIST_COUNT);

private void parseMus(){
     musicLists = new ArrayList<List<Mp3>>(LIST_COUNT);
     ...
    //gettin mp3 list id,filename, length
    ...
    musicLists.get(listNr).add(new Mp3(id,filename,length));
...
}

but it gives me errors:

     03-15 21:01:36.030: E/AndroidRuntime(3393): java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0

Second quwestion! now I edited code and have no errors... BUT now what i get is that all list is filled with same lists.. so when i try to see:

        for(int i =0; i< Settings.musicLists.size();i++){
        if(D)Log.e("visio added","MP3 file muslist nr="+i+"= "+Settings.musicLists.get(i)); 
    } 

I get 8 identical rows shown... what I do wrong?

5
  • 1
    are you sure that you want a list of lists containing Mp3 Objects? Commented Mar 15, 2012 at 20:13
  • a could make 8 lists.. but think that ArrayList would be better and easyer to use... Commented Mar 15, 2012 at 20:15
  • it says Invalid index 4, size is 0 .. while I am creating a new ArrayList with LIST_COUNT =8.. Commented Mar 15, 2012 at 20:17
  • Yes but you're not putting anything into that first arrayList so it's size is 0 (with a capacity of 8) Commented Mar 15, 2012 at 20:25
  • I am bit confused... To add after for (i=0;i<LIST_COUN;i++) musicLists.add(foo) when i put musicLists.get(listNr).add(new Mp3(id,filename,length)); it should put it in the list in listNr ... or Im I understanding it wrong? Commented Mar 15, 2012 at 20:31

2 Answers 2

5

musicLists = new ArrayList<List<Mp3>>(LIST_COUNT);

You seem to think that this line creates a list of 8 lists. However, what it really does is to create an empty list of lists with initial capacity 8. That is, it doesn't actually populate any lists into musicLists.

In order for this you do what you expect it to, you need to initialize musicLists and then use musicLists.add(foo) to add 8 List<Mp3> objects to it.

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

4 Comments

you mean I have to for(int i = 0; <8;i++) musicList.add(foo); ??
Or: for (int i =0; i < LIST_COUNT; i++)
now I get 03-15 21:24:57.060: E/AndroidRuntime(4397): java.lang.NullPointerException
Because nothing is in your original list. Unless you're populating it outside of the code you've posted?
1

As VeeArr said you need to fill the list with lists before you can get the lists.

// this creates an empty list of initial capacity 8
musicLists = new ArrayList<List<Mp3>>(8);
//  musicList.size() is still 0
// we can add as much items as we want, the list will dynamically grow.
for (int i = 0; i < 8; i++) {
    List<Mp3> emptySubList = new ArrayList<Mp3>();
    // emptySubList.size() is 0 each as well.
    musicList.add(emptySubList);
}
//  musicList.size() is 8 now.

// do your stuff...
musicLists.get(listNr).add(new Mp3(id,filename,length));

The initial capacity you can specify for an ArrayList is just a way to improve memory consumption.

ArrayLists can dynamically grow unlike array[]s. They do that by internally keeping an array[] where the data you put in your ArrayList is actually stored. If the size they need gets bigger than the array[] can hold they create a bigger array[] and copy the content to that new version (afaik the size is always doubled).

Thats nice because you don't need to worry about doing that yourself. But copying arrays is expensive. Therefore you can define an initial size to hint your ArrayList that it should expect a certain number of elements. It's usually fine if you don't give an initially capacity unless you really know how much items you need.

1 Comment

Second quwestion! now I edited code and have no errors... BUT now what i get is that all list is filled with same lists.. so when i try to see: for(int i =0; i< Settings.musicLists.size();i++){ if(D)Log.e("musiclist added","MP3 file muslist nr="+i+"= "+Settings.musicLists.get(i)); } I get 8 identical rows shown... what I do wrong?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.