2

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?

6
  • 1
    you are using arrayList, why you need to add in loop, you can directly add in list using , list.nameList.add("d"); Commented Sep 28, 2019 at 5:24
  • sorry, I edit the code. What I want to try to achieve is if the user want to add to index 0 while it's already filled, it doesn't shift the index. a,b,c is still index 0-2 on nameList, while d is stored into index 4 instead of index 0. Commented Sep 28, 2019 at 5:30
  • The number in arraylist constructor specifies capacity, not size: docs.oracle.com/javase/8/docs/api/java/util/… Commented Sep 28, 2019 at 5:31
  • No need of using loop and while. Simply write nameList.add(0,"d"); Commented Sep 28, 2019 at 5:34
  • 1
    you are confusing array with ArrayList: 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, use add("d") - in example will be added at (new) index 3 Commented Sep 28, 2019 at 5:53

3 Answers 3

4

As in the code there is no need to use loop to add element into arraylist. You can directly add the element into arraylist using the add() method.

Arraylist automatically adds the element to the next available index. Refer documentation https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html.

Code to add new element to arraylist is as below

ArrayList<String> nameList = new ArrayList<String>(5);

nameList.add(0,"a");
nameList.add(1,"b");
nameList.add(2,"c");
nameList.add(3, "d");

System.out.println(nameList);

System.out.println(nameList.get(3));
Sign up to request clarification or add additional context in comments.

1 Comment

not need to use the index in add (and part of question, I assume, that the last add is not at fixed index, the first 3 values are just an example)
1

Try below code

ArrayList<String> nameList = new ArrayList<String>(5);

nameList.add("a");
nameList.add("b");
nameList.add("c");
nameList.add("d");

System.out.println(nameList.get(2));

There is no need to give index. It will automatically insert element to next available index

Comments

0

The problem is ill defined, as there are no empty indices in your input list.

ArrayList<String> nameList = new ArrayList<String>(5);

nameList.add(0,"a");
nameList.add(1,"b");
nameList.add(2,"c");

The snippet above creates list of size 3, not 5. The number in arraylist constructor specifies capacity, not size: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList-int-

You assume that the index 3 holds empty value. It does not - it is already out of bounds.

Side note: define what empty means for you: String.isEmpty or equal to null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.