【发布时间】:2021-01-29 03:04:44
【问题描述】:
我正在做一个迭代器的练习,我有 2 个类“菜单”和“菜”菜单。
Class Menu 代表餐厅的菜肴集合。该实现是在一个基本的 ArrayList of Dishes 上完成的。 ArrayList 迭代器用于迭代。
我的疑问是我是否可以在同一个类中使用 for-each 和不同的迭代器以及如何使用。
菜单类:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Menu implements Iterable<Plat> {
private List<Plat> plats=new ArrayList<Plat>();
@Override
public Iterator<Plat> iterator() {
return new IteratorVegetaria(plats);
}
public Iterator<Plat> iteratorCarnivoro() {
return new IteratorCarnivoro(plats);
}
public void afegirPlat(Plat p){
plats.add(p);
}
public static void main(String[] args) {
Menu m=new Menu();
Iterator<Plat> c=m.iteratorCarnivoro();
Iterator<Plat> v=m.iterator();
m.afegirPlat(new Plat("soup",1.5,true,false,true));
m.afegirPlat(new Plat("pizza",2.5,true,false,false));
m.afegirPlat(new Plat("chicken",5.80,false,true,true));
m.afegirPlat(new Plat("salad",2.5,true,false,true));
System.out.println("Non-vegetarian menu");
// Iteration without for-each
while (c.hasNext()){
System.out.println(c.next());
}
// Iteration with for-each (doesn't work and only show vegetarian dishes)
for(Plat p: m){
System.out.println(p);
}
System.out.println("Vegetarian menu:");
// Iteration without for-each
while(v.hasNext()){
System.out.println(v.next());
}
/* Iteration with for-each*/
for(Plat p: m){
System.out.println(p);
}
}
public class IteratorVegetaria implements Iterator<Plat>{
private List<Plat>plats;
private int actual=0;
public IteratorVegetaria(List<Plat>p){
plats = p;
}
// With this method we define that has next iterate as long as it finds an object that is vegetarian and adds it to the array.
@Override
public boolean hasNext() {
boolean hiHaNext = false;
int i = actual;
while (i<plats.size() && !hiHaNext){
if (plats.get(i).isVegetaria()) hiHaNext = true;
else i++;
}
return hiHaNext;
}
//With this method we define that next will iterate and jump to the next position when it finds a non-vegetarian dish
@Override
public Plat next() {
Plat p=plats.get(actual++);
while (!p.isVegetaria()){
p=plats.get(actual++);
}
return p;
}
}
public class IteratorCarnivoro implements Iterator<Plat>{
private List<Plat>plats;
private int actual=0;
public IteratorCarnivoro(List<Plat>p){
plats = p;
}
// With this method we define that has next iterate as long as it finds an object that is non-vegetarian and adds it to the array.
@Override
public boolean hasNext() {
boolean hiHaNext = false;
int i = actual;
while (i<plats.size() && !hiHaNext){
if (!plats.get(i).isVegetaria()) hiHaNext = true;
else i++;
}
return hiHaNext;
}
//With this method we define that next will iterate and jump to the next position when it finds a vegetarian dish
@Override
public Plat next() {
Plat p=plats.get(actual++);
while (p.isVegetaria()){
p=plats.get(actual++);
}
return p;
}
}
}
菜品类:
public class Plat {
private String nom;
private double preu;
private boolean vegetaria;
private boolean apteCeliacs;
private boolean baixCalories;
public Plat(String nom, double preu, boolean vegetaria, boolean apteCeliacs, boolean baixCalories) {
this.nom = nom;
this.preu = preu;
this.vegetaria = vegetaria;
this.apteCeliacs = apteCeliacs;
this.baixCalories = baixCalories;
}
public boolean isVegetaria() {
return vegetaria;
}
@Override
public String toString() {
String missatge = "Plat{" +
"nom='" + nom + '\'' +
", preu=" + preu +
", apteCeliacs=" + apteCeliacs +
", baixCalories=" + baixCalories +
", vegetaria=";
if (isVegetaria()){
return missatge+" es vegetaria";
}else{
return missatge+" no es vegetaria";
}
}
}
所以基本上当我运行代码时,它会显示:
Non-vegetarian menu
Plat{nom='chicken', preu=5.8, apteCeliacs=true, baixCalories=true, vegetaria= no es vegetaria
Plat{nom='soup', preu=1.5, apteCeliacs=false, baixCalories=true, vegetaria= es vegetaria
Plat{nom='pizza', preu=2.5, apteCeliacs=false, baixCalories=false, vegetaria= es vegetaria
Plat{nom='salad', preu=2.5, apteCeliacs=false, baixCalories=true, vegetaria= es vegetaria
Vegetarian menu:
Plat{nom='soup', preu=1.5, apteCeliacs=false, baixCalories=true, vegetaria= es vegetaria
Plat{nom='pizza', preu=2.5, apteCeliacs=false, baixCalories=false, vegetaria= es vegetaria
Plat{nom='salad', preu=2.5, apteCeliacs=false, baixCalories=true, vegetaria= es vegetaria
Plat{nom='soup', preu=1.5, apteCeliacs=false, baixCalories=true, vegetaria= es vegetaria
Plat{nom='pizza', preu=2.5, apteCeliacs=false, baixCalories=false, vegetaria= es vegetaria
Plat{nom='salad', preu=2.5, apteCeliacs=false, baixCalories=true, vegetaria= es vegetaria
【问题讨论】:
-
就像阅读代码时的旁白一样,
c、m和v等变量名称非常难以描述,使您的代码更难阅读。考虑使用完整的单词和短语而不是单个字母。
标签: java oop iteration iterable