可以向我描述为什么我的迭代器不起作用。JDeveloper 说我必须为 Iterator 创建一个方法,但我不知道程序对此意味着什么。因此,我请求您的帮助。该程序如下所示:
测试顺序:
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;
}
}
订单注册:
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);
}
}
}