2013-01-10 73 views
-1

可以向我描述爲什麼我的迭代器不起作用。 JDeveloper說我必須爲Iterator創建一個方法,但我不知道程序的含義。所以我請求你的幫助。該方案是這樣的:爲迭代器創建方法

TestOrder:

package hej; 

import java.util.*; 

public class TestOrder { 


public static void main(String[] args) { 



OrderRegister orderregister = new OrderRegister(); 


Order order1 = new Order("123","Kop"); 
Order order2 = new Order("456","Salj"); 
Order order3 = new Order("789","Kop"); 


orderregister.addOrder(order1); 
orderregister.addOrder(order2); 
orderregister.addOrder(order3); 

System.out.println(orderregister.sokOrder("123").getKopsalj()); 
orderregister.raderaOrder("456"); 
Order tmpOrder = orderregister.sokOrder("456"); 
if (tmpOrder == null) { 
    System.out.println("Fungerar!"); 
} 
else{ 
    System.out.println("Why u lie?"); 
} 
System.out.println(orderregister.sokOrder("123").getKopsalj()); 
orderregister.sokOrder("123").setKopsalj("Salj"); 
System.out.println(orderregister.sokOrder("123").getKopsalj()); 



Iterator<Order> i=orderregister.iterator(); 
    while(i.hasNext()){ 
     System.out.println(i.next()); 
    } 
} 
} 

訂單:

package hej; 

import java.util.Iterator; 

public class Order { 
private String ordernr; 
private String kopsalj; 

public Order(String newOrdernr, String newKopsalj) { 
    setOrdernr(newOrdernr); 
    setKopsalj(newKopsalj); 
} 

public void setOrdernr(String ordernr) { 
    this.ordernr = ordernr; 
} 

public String getOrdernr() { 
    return ordernr; 
} 

public void setKopsalj(String kopsalj) { 
    this.kopsalj = kopsalj; 
} 

public String getKopsalj() { 
    return kopsalj; 
} 

public String toString() 
{ 
    return "Order: " + this.ordernr+", "+"Manover: " 
          +this.kopsalj; 
} 
} 

OrderRegister:

package hej; 

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

public class OrderRegister { 

private ArrayList<Order> orderArrayList; 

public OrderRegister() { 
    orderArrayList = new ArrayList<Order>(); 
} 



// Lagg till Order 

public void addOrder(Order newOrder) { 
    orderArrayList.add(newOrder); 
} 




// Sok Order 

public Order sokOrder(String ordernrSok) { 
    Order tmpOrder = null; 
    int i = 0; 
    boolean found = false; 
    while (i < orderArrayList.size() && !found) { 
     tmpOrder = orderArrayList.get(i); 
     if (tmpOrder.getOrdernr().equals(ordernrSok)) { 
      found = true; 
     } 
     i++; 
    } 
    if (!found) { 
     tmpOrder = null; 
    } 
    return tmpOrder; 
} 



// Ta bort Order 

public void raderaOrder(String ordernrRadera) { 
    Order tmpOrder = null; 
    int i = 0; 
    boolean found = false; 
    while (i < orderArrayList.size() && !found) { 
     tmpOrder = orderArrayList.get(i); 
     if (tmpOrder.getOrdernr().equals(ordernrRadera)) { 
      orderArrayList.remove(i); 
      found = true; 
     } 
     i++; 
    } 
} 



// andra Orderuppgifter 

public void setOrderUppgifter(String ordernr, String newKopsalj){ 
    Order order = sokOrder(ordernr); 
    if (order != null) { 
     order.setKopsalj(newKopsalj); 
    } 
} 
} 
+2

你可以將代碼片段減少到真正需要的嗎?另外,你是否嘗試給你的類添加一個'iterator()'方法? (或者,可以擴展*'ArrayList'而不是包裝一個!) –

+0

它究竟說了什麼?請添加確切的錯誤 – amit

回答

0

讓OrderRegister實現Iterable接口和實現)迭代器比()

那裏你纔可以委託給orderArrayList.iterator(

3

你需要讓OrderRegister類實現IterableT是迭代器數據類型,在你的情況下是Order。 所以,你得到:

public class OrderRegister implements Iterable<Order> 

Iterable界面,需要你定義一個iterator()方法,它返回一個Iterator<E>對象。在你的情況,你不需要創建這樣的類,因爲你可以直接抓取迭代器對象,您ArrayList

public Iterator<Order> iterator() { 
    return orderArrayList.iterator(); 
} 

現在你可以使用你的類以這樣的方式

for(Order order: orderregister) { 
    System.out.println(order.getKöpsälj()); 
} 

注:您while -loop也仍然可以工作

0

我認爲你的問題是這一行:

Iterator<Order> i=orderregister.iterator(); 

,因爲您的班級中沒有iterator()的方法OrderRegister

兩個解決方案:

A)添加方法iterator()(它很可能只是做return orderArrayList.iterator();

B)以外,包裝的ArrayList延長吧!

public class OrderRegister extends ArrayList<Order> { 
    // No more orderArrayList! 
}