gpt4 book ai didi

java - 列表未打印出其中一个变量

转载 作者:行者123 更新时间:2023-11-29 03:20:15 25 4
gpt4 key购买 nike

下面的代码将半径和值存储在其列表中。但是当我去打印值时,它只打印半径而不是其他变量值。我不明白为什么它不打印出来。

这是我的代码:

public class AddingBoth {

private double radius;
private double value;
private List<AddingBoth> list = new ArrayList<AddingBoth>();

public AddingBoth(double radius, double value){
this.radius = radius;
this.value = value;
list.add(this);
}

public List<AddingBoth> getList(){
return list;
}

public double getRadius(){
return radius;
}

public double getValue(){
return value;
}

public void print(){
Iterator<AddingBoth> iter = list.iterator();
while(iter.hasNext()){
System.out.println(iter.next().getRadius()+" ");
}

System.out.println("This is the value; ");
while(iter.hasNext()){
System.out.println("Value: "+iter.next().getValue());
}
}
}

这就是我给类(class)打电话的地方:

   AddingBoth ab = null;

for(int i =0; i< 256; i++){
int y=0;
while(y<256){
//image.getPixel(i, y);
String x = image.getLocationAsString(i, y);
String n = image.getValueAsString(i, y);
//System.out.println(x);

String delim = ", value=";
String [] tokens = n.split(delim);
double num = Double.parseDouble(tokens[1]);

//if(image.getR() < 1.43){
String [] t = x.split("r=");
String[] b = t[1].split(" mm/c");
//System.out.print("Meet b: "+b[0]);
double radius = Double.parseDouble(b[0]);

String [] theta = x.split("theta= ");
String [] token2 = theta[1].split(Character.toString(IJ.degreeSymbol));
float thetaNum = Float.parseFloat(token2[0]);
//System.out.print(" This is the theta value: "+thetaNum+" ");

ab = new AddingBoth(radius, num);
ab.print();
y++;
}
}

最佳答案

由于您已经遍历了迭代器直到它没有更多的值,所以您已经用完了它,并且您永远不会进入第二个 while 循环。

避免这种情况的一种方法是在第二个循环中使用新的迭代器:

public void print(){
Iterator<AddingBoth> iter = list.iterator();
while(iter.hasNext()){
System.out.println(iter.next().getRadius()+" ");
}

System.out.println("This is the value; ");
iter = list.iterator(); // new iterator!
while(iter.hasNext()){
System.out.println("Value: "+iter.next().getValue());
}
}

关于java - 列表未打印出其中一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24087285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com