Java 列表未打印出其中一个变量
下面的代码将半径和值存储在其列表中。但是当我去打印值时,它只打印半径,而不是其他变量值。我不明白为什么不打印出来 这是我的密码:Java 列表未打印出其中一个变量,java,list,arraylist,Java,List,Arraylist,下面的代码将半径和值存储在其列表中。但是当我去打印值时,它只打印半径,而不是其他变量值。我不明白为什么不打印出来 这是我的密码: public class AddingBoth { private double radius; private double value; private List<AddingBoth> list = new ArrayList<AddingBoth>(); public AddingBoth(double radius, double
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());
}
}
}
公共类同时添加{
私人双半径;
私人双重价值;
私有列表=新的ArrayList();
公共添加二者(双半径,双值){
这个半径=半径;
这个值=值;
列表。添加(此);
}
公共列表getList(){
退货清单;
}
公共双getRadius(){
返回半径;
}
公共双getValue(){
返回值;
}
公开作废印刷品(){
迭代器iter=list.Iterator();
while(iter.hasNext()){
System.out.println(iter.next().getRadius()+);
}
System.out.println(“这是值;”);
while(iter.hasNext()){
System.out.println(“值:+iter.next().getValue());
}
}
}
这就是我给我的班级打电话的地方:
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++;
}
}
添加两个ab=null;
对于(int i=0;i<256;i++){
int y=0;
(y您使用了两次迭代器,这导致了您的问题。基本上,您会遍历列表,然后当您在末尾时,您会询问列表中是否有其他元素(没有)。当您到达末尾时,列表不会重新启动
使用一个while循环,而不是两个while循环
public void print(){
Iterator<AddingBoth> iter = list.iterator();
AddingBoth temp;
while(iter.hasNext()){
temp = iter.next();
System.out.println(temp.getRadius()+" ");
System.out.println("Value: "+temp.getValue());
}
}
public void print(){
迭代器iter=list.Iterator();
加上两个温度;
while(iter.hasNext()){
temp=iter.next();
System.out.println(temp.getRadius()+);
System.out.println(“值:+temp.getValue());
}
}
您使用了两次迭代器,这导致了您的问题。基本上,您会一直浏览列表,然后在最后询问列表中是否有其他元素(没有)。当您到达末尾时,列表不会重新启动
使用一个while循环,而不是两个while循环
public void print(){
Iterator<AddingBoth> iter = list.iterator();
AddingBoth temp;
while(iter.hasNext()){
temp = iter.next();
System.out.println(temp.getRadius()+" ");
System.out.println("Value: "+temp.getValue());
}
}
public void print(){
迭代器iter=list.Iterator();
加上两个温度;
while(iter.hasNext()){
temp=iter.next();
System.out.println(temp.getRadius()+);
System.out.println(“值:+temp.getValue());
}
}
您的打印方法应该简化,您可能真的需要这样的打印方法-
public void print(){
for (AddingBoth ab : list) {
// Is your radius really a String?
System.out.println(String.valueOf(ab.getRadius()) + " = " + ab.getValue());
}
// As the other answers point out you had already exhausted the iterator.
System.out.flush();
}
您的打印方法应该简化,您可能真的需要这样的打印方法-
public void print(){
for (AddingBoth ab : list) {
// Is your radius really a String?
System.out.println(String.valueOf(ab.getRadius()) + " = " + ab.getValue());
}
// As the other answers point out you had already exhausted the iterator.
System.out.flush();
}
因为您已经在迭代器上运行了一段时间,直到它没有更多的值,所以您已经耗尽了迭代器,并且永远不会进入第二个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());
}
}
public void print(){
迭代器iter=list.Iterator();
while(iter.hasNext()){
System.out.println(iter.next().getRadius()+);
}
System.out.println(“这是值;”);
iter=list.iterator();//新的迭代器!
while(iter.hasNext()){
System.out.println(“值:+iter.next().getValue());
}
}
因为您已经在迭代器上运行了一段时间,直到它没有更多的值为止,所以您已经耗尽了迭代器,并且您永远不会进入第二个循环
避免这种情况的一种方法是对第二个循环使用新的迭代器:
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());
}
}
public void print(){
迭代器iter=list.Iterator();
while(iter.hasNext()){
System.out.println(iter.next().getRadius()+);
}
System.out.println(“这是值;”);
iter=list.iterator();//新的迭代器!
while(iter.hasNext()){
System.out.println(“值:+iter.next().getValue());
}
}
我非常确定您需要去掉第二个`iter.hasNext()块。您希望两个print语句都在同一个循环中。我非常确定您需要去掉第二个`iter.hasNext()块。您希望两个print语句都在同一个循环中。这将失败,因为您调用了iter.next()
两次。您需要一个临时变量。这将失败,因为您调用了iter.next()
两次。您需要一个临时变量。谢谢。这很有效。我不知道我是否在正确的轨道上,但我想得到所有半径和振幅,然后我想在半径中查找任何重复项,如果是,我想添加值(振幅)半径的大小,并删除重复项。我认为列表将是一个好主意,但我不知道它是否会起作用。你对如何做有什么建议,或者这是最好的方法吗?@selena很棒。这是一个新问题(所以问一个)。但是,我不会在这里问,我会在该网站上问,这不只是数学问题吗?“我假设得到所有半径和振幅,然后我假设寻找半径中的任何重复项,如果是,我假设添加半径的值(振幅)"对我来说,这听起来像是数学。尽管我有可能证明自己对这个问题一无所知,但半径的振幅是多少?它们现在是用度数和弧度以外的单位来测量的吗?但我需要编写代码。我使用了链表、数组表、if-else和for循环,其中一些是成功的,但效率不高。我只是在寻找一个高效的编程方式。谢谢。这很有效。我不知道我是否在正确的轨道上,但我想得到所有的半径和振幅,然后我想寻找半径中的任何重复项,如果是,我想添加值(振幅)半径的大小,并删除重复项。我认为列表将是一个好主意,但我不知道它是否会起作用。你对如何做有什么建议,或者这是最好的方法吗?@selena很棒。这是一个新问题(所以问一个)。但是,我不会在这里问,我会在该网站上问,这不只是数学问题吗?“我假设得到所有的半径和振幅,然后我假设寻找半径中的任何重复项,如果它们是,我假设将半径的值(振幅)相加”听起来像是m的数学