可以向我描述爲什麼我的迭代器不起作用。 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);
}
}
}
你可以將代碼片段減少到真正需要的嗎?另外,你是否嘗試給你的類添加一個'iterator()'方法? (或者,可以擴展*'ArrayList'而不是包裝一個!) –
它究竟說了什麼?請添加確切的錯誤 – amit