I want to add value to arraylist to next empty index found in the arraylist.
Let's say I have ArrayList with length of 5. Index 0-2 of the ArrayList is already filled. I want to add another value to index 3. I have this code but it doesn't work (returns index out of bound exception).
ArrayList<String> nameList = new ArrayList<String>(5);
nameList.add(0,"a");
nameList.add(1,"b");
nameList.add(2,"c");
for (int i = 0; i < nameList.size(); i++) {
while (!nameList.get(i).isEmpty()) i++;
nameList.add(0,"d");
}
System.out.println(nameList);
Are there any way to achieve this?
arraywithArrayList: there is no "empty index" in that list; the size of the list is just 3 (System.out.println(nameList.size()). To add to the end of the list, useadd("d")- in example will be added at (new) index 3