1

what is the correct way to iterate through an array list without getting an exception thrown? I tried this:

        while(packages.get(i)!=null)
        {
            if(packages.get(i).equals(z))
            {
                packages.remove(i);
                this.setNumPack(this.getNumPack()-1);
            }
            i++;
        }

It throws exception when the index is bigger than the size of the array list.I also tried to iterate as long as the iterator was smaller than the size of the array but it didnt help. Thanks

7 Answers 7

1

There are many ways to iterate and remove, but in your specific code the solution is not to increment the index if you remove an element (since removing an element decrements the indices of all the elements that come after the removed element) :

    while(packages.get(i)!=null)
    {
        if(packages.get(i).equals(z))
        {
            packages.remove(i);
            this.setNumPack(this.getNumPack()-1);
        } else {
            i++;
        }
    }

Of course packages.get(i) would still throw an exception when i reaches packages.size(), so a for loop would be better:

    for (int i = 0; i < packages.size(); i++)
    {
        if(packages.get(i).equals(z))
        {
            packages.remove(i);
            this.setNumPack(this.getNumPack()-1);
            i--;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0
       for (int i = 0; i < packages.size(); i++) {
            if(packages.get(i).equals(z)) {
                packages.remove(i);
                this.setNumPack(this.getNumPack()-1);
            }
        }

Comments

0

usual way :

for (int i = 0; i < list.size(); i++)

'new' way :

for ('your type' item : list)

Comments

0

You can check if the list is null or if the list has null elements... those are 2 compl. different things

an iterator can be implemeted, but you need to explicitly check null values...

    List<Integer> l = Arrays.asList(1, null, 3, 4, 5, null);

    Iterator<Integer> it = l.iterator();

    while (it.hasNext()) {
        Integer integer = (Integer) it.next();
        if (integer != null) {
            System.out.println("the element is holding: " + integer);
        } else {
            System.out.println("the element is null");
        }
    }

or a classical enhanced loop

    List<Integer> l = Arrays.asList(1, 2, 3, 4, 5, null);
    if (l != null) {
        for (Integer integer : l) {
            if (integer != null) {
                System.out.println("the element is holding: " + integer);

            } else {
                System.out.println("the element is null");
            }
        }
    } else {
        System.out.println("the list is null");
    }

Comments

0

You can use any one of them

//Loop Arraylist using foreach loop of JDK1.5
                System.out.println("ArrayList Loop Example using foreach loop of JDK 1.5");
        for(String element: loopList){
            System.out.println(element);
        }




 //Loop Arraylist using simple for loop and size method
            System.out.println("ArrayList Loop Example using for loop and size()");
    for(int i=0; i<loopList.size(); i++){
        System.out.println(loopList.get(i));
    }



//Iterate Arraylist using iterator and while loop in Java
           System.out.println("ArrayList Loop Example using Iterator and while loop");
    Iterator<String> iterator = loopList.iterator();
    while(iterator.hasNext()){
        System.out.println(iterator.next());
    }





 //Iterate Arraylist using ListIterator and while loop in Java
                System.out.println("ArrayList Loop Example using ListIterator and while loop");
        ListIterator<String> listIterator = loopList.listIterator();
        while(listIterator.hasNext()){
            System.out.println(listIterator.next());
        }

Comments

0

You also can use Java 8 stream API

List<Integer> list = Arrays.asList(1, 3, 4, 5);
list.stream().forEach(System.out::println);

Comments

0

You will run into problems sooner or later by iterating from head to tail in an arraylist if you plan to remove elements along the way. The trick is to iterate from tail to head since all of the reordering being done in the list is done from the removed index to the end of the list, - not on indexes before the one removed.

pseudo-code:

for(int i = list.size()-1; i >=0; i--){
    list.remove(i);
}

Comments

Your Answer

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