简体   繁体   中英

Adding, removing and printing from ArrayList with Iterator

First of all I want t say that I am aware of other easier and better loops for this kind of work, but I just want to practise a little with Iterator. So, I am looping through an ArrayList and I want to add and remove elements in it with iterator but when I run the program I get no output on screen. This is what I have:

public class Orders { 
    ArrayList<String> list = new ArrayList<>();
    Iterator it = list.iterator();

    public void addOrder(String s) {
        while(it.hasNext()){
            String st = (String) it.next();
            if(s.equals(st)){
                System.out.println("The order '"+s+"' already exists.");
            }
            else{
                list.add(s);
                System.out.println("Order '"+s+"' was added.");
            }
        }

    }

    public void deleteOrder(String s) {
        while(it.hasNext()){
            String st = (String) it.next();
            if(s.equals(st)){
                list.remove(s);
                System.out.println("The order '"+s+"' was deleted succesfully.");
            }
            else{
                System.out.println("The order '"+s+"' was not found.");
            }
        }
    }

    public void showList(){
        while(it.hasNext()){
            String st = (String) it.next();
            System.out.println("Order: '"+st+"'. \n");
        }
    }
}



public class RemoveOrder {

  private Orders ord;
  public RemoveOrder( Orders o ) {
    ord = o;
  }
  public void remove() {  
      ord.deleteOrder( "jk123" );
      ord.deleteOrder( "jk125" );
      ord.showList();
  }
}


public class AddOrder {

    private Orders ord;

    public AddOrder( Orders o ) {
        ord = o;
    }

    public void add() {
        ord.addOrder( "jk123" );
        ord.addOrder( "jk124" );
        ord.addOrder( "jk125" );
        ord.addOrder( "jk126" );
        ord.showList();
    }
}


public class IteratorRun implements Runnable {


    Orders ord = new Orders();
    AddOrder ao = new AddOrder(ord);
    RemoveOrder ro = new RemoveOrder(ord);

    public void run() {
        try {
            ao.add();
            ro.remove();
        } catch (Exception e) {
        }
    }
}

And in main:

Thread t1 = new Thread(new IteratorRun());
t1.start();
  1. Check it.hasNext() in addOrder. First time array list is empty and hence it.hasNext will be false.
  2. If we try to add value to araylist while using iterator it will throw java.util.ConcurrentModificationException

In most of the places similar code is present addOrder/deleteOrder this will cause this exception.

example : Following code will be throwing ConcurrentModificationException.

  ArrayList al = new ArrayList(); al.add("test"); Iterator iter = al.iterator(); while(iter.hasNext()) { System.out.println(iter.next()); al.add("test"); } 

I have updated the Orders code

  1. You need to re-initiate the iterator from list, as list get be modified
  2. logic for checking whether value already present in the list is modified.


    import java.util.ArrayList;
    import java.util.Iterator;

    public class Orders { 
        ArrayList list = new ArrayList();
        Iterator it = null;

        public void addOrder(String s) {
            it = list.iterator();
            while(it.hasNext()){
                String st = (String) it.next();
                if(s.equals(st)){
                    System.out.println("The order '"+s+"' already exists.");
                    return;
                }

            }


                list.add(s);
                System.out.println("Order '"+s+"' was added.");    


        }

        public void deleteOrder(String s) {
            it = list.iterator();
            while(it.hasNext()){
                String st = (String) it.next();
                if(s.equals(st)){
                    list.remove(s);
                    System.out.println("The order '"+s+"' was deleted succesfully.");
                    return ;
                }

            }
                System.out.println("The order '"+s+"' was not found.");

        }

        public void showList(){
            it = list.iterator();
            while(it.hasNext()){
                String st = (String) it.next();
                System.out.println("Order: '"+st+"'. \n");
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM