使用迭代器打印和更改项目

时间:2018-10-02 00:11:42

标签: java

这就是我的规格。
// ToDo 7-遍历剩余的鱼,并显示您可以分别获取每个项目。       System.out.println(“显示您的吸气剂正在工作:\ n”);

// ToDo 8-遍历剩余的鱼并更改所有数据       System.out.println(“显示您的设置员正在工作:\ n”);

我认为我已经正确地完成了#7的工作,但是我不确定,因为我没有在其中使用getters并且我不确定如何使用迭代器来完成这项工作,所以8号ive根本不知道如何开始。这是我的课程

FISHDATA类

   public class FishData {
    private String species;  //species of fish
    private int minSize;  //min size allowed in inches
    private String season;  //fishing season
    private int limit;  //number
    private Boolean willEat;
    //constructor
    FishData(String s1, int m, String s2, int I, Boolean w){
        species = s1;
        minSize = m;
        season = s2;
        limit = I;
        willEat = w;
    }
    public String toString(){
        return species + "" + minSize + "" + season + "" + limit + "" + willEat;
    }
    public String getName(){
        return this.species;
    }
    public String getSpecies(){
        return this.species;
    }
    public String getSeason(){
        return this.season;
    }
    public Boolean getWillEat(){
        return this.willEat;
    }

}

主要班级

   import java.util.Collections;
import java.util.LinkedList;
import java.util.Iterator;
public class GoneFishin {
    public static void main(String[] args) {
        //create linked list of fish data called fl
        LinkedList<FishData> fl = new LinkedList<FishData>();
        //Here are a few data items
        fl.add(new FishData("American Eel ", 9, "Summer/Spring ", 25, false));
        fl.add(new FishData("Hammerhead Shark ", 0, "All Year ", 36, false));
        fl.add(new FishData("Horseshoe Crab ", 7, "All Year except May ", 60, false));
        fl.add(new FishData("Haddock ", 18, "All Year ", 0, true));
        fl.add(new FishData("Tautog ", 16, "late Spring to end of year ", 3, true));

        for (FishData element : fl) {
            System.out.println(element);
        }
        System.out.print("SIZE OF LISTt: ");
        System.out.println(fl.size());

        System.out.print("SECOND ELEMENT IN LIST: ");
        System.out.println(fl.get(1));

        System.out.print("LAST ELEMENT IN LIST: ");
        System.out.println(fl.getLast());

        Iterator<FishData> itr = fl.iterator();

        System.out.println("PRINTING ALL ELEMENTS USING ITERATOR");
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
        while (itr.hasNext()) {
            FishData b = (FishData) itr.next();
            if (b.getWillEat()== false) {
                itr.remove();
            }
        }
        System.out.println("FISH THAT ARE GOOD TO EAT");
        for (FishData element : fl) {
            System.out.println(element);
        }
    }
}

0 个答案:

没有答案