使用Iterator从ArrayList添加,删除和打印

时间:2013-12-26 15:16:09

标签: java arraylist iterator

首先,我想说我知道其他更容易和更好的循环用于这种工作,但我只是想用Iterator练习一下。所以,我循环遍历一个ArrayList,我想用迭代器添加和删除它中的元素,但是当我运行程序时,屏幕上没有输出。这就是我所拥有的:

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) {
        }
    }
}

主要是:

Thread t1 = new Thread(new IteratorRun());
t1.start();

2 个答案:

答案 0 :(得分:0)

  1. 在addOrder中检查it.hasNext()。第一次数组列表为空 因此it.hasNext将是假的。
  2. 如果我们尝试在使用迭代器时为araylist添加值,它会 throw java.util.ConcurrentModificationException
  3. 在大多数地方存在类似的代码addOrder / deleteOrder这会导致此异常。

    示例:以下代码将抛出ConcurrentModificationException。

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

答案 1 :(得分:0)

我已更新订单代码

  1. 你需要从list中重新启动迭代器,因为list get 改性
  2. 用于检查列表中是否已存在的值的逻辑 修改。
  3.  
    
        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");
                }
            }
        }