这是我的规格说我需要做的最后一件事 // ToDo 8-遍历剩余的鱼并更改所有数据 System.out.println(“显示您的设置员正在工作:\ n”);
我不确定该怎么做,我试图测试.set方法的链表,但令我感到困惑的是,每个元素中都有多个字段,所以我不能只写fl.add(索引,元素),我只需要知道如何更改元素的每个字段中的数据,然后可以进行另一个迭代器并进行其余操作即可。这是我的代码
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 getSpecies(){
return this.species;
}
public String getSeason(){
return this.season;
}
public Boolean getWillEat(){
return this.willEat;
}
public int getminSize() {
return this.minSize;
}
public int getLimit() {
return this.limit;
}
public void setSpecies(String species) {
this.species = species;
}
public void setSeason(String season) {
this.season = season;
}
public void setWillEat(Boolean willEat) {
this.willEat = willEat;
}
public void setminSize(int minSize) {
this.minSize = minSize;
}
public void setlimit(int limit) {
this.limit = limit;
}
}
主班
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> itr2 = fl.iterator();
System.out.println("PRINTING ALL ELEMENTS USING ITERATOR");
while (itr2.hasNext()) {
System.out.println(itr2.next());
}
Iterator<FishData> itr = fl.iterator();
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);
}
fl.set(1,"Shrekfish", 19, "whenever it wants", 7, false);
}
}
我知道最后的fl.set无法正常工作,给我一个错误,我只是想弄清楚它是如何工作的,所以我正在测试它。
答案 0 :(得分:1)
您需要将其更改为
fl.set(1, new FishData("Shrekfish", 19, "whenever it wants", 7, false));
*如果我正确理解了您的问题。这会将索引1
处的元素替换为新的对象
如果您只想更新单个元素,则
for (FishData element : fl) {
element.setminSize (28); // or whatever
System.out.println(element);
}
答案 1 :(得分:1)
在fl
(这是一个链接列表)中,通过删除某些鱼来进行缩减之后,似乎任务是迭代所有剩余的鱼,并在其上调用设置器。因此,类似:
for (FishData element : fl) {
element.setSeason("Winter, Summer, Spring, or Fall");
}
如果我理解这个问题,我们的想法是更新每个剩余元素的值。