在 Java 中迭代 SortedSet
Iterating through SortedSet in Java
我正在尝试为存储在 SortedSet 中的双精度值创建间隔。
下面是我的代码:
public class Trail {
public static void main(String[] args) {
SortedSet<Double> val = new TreeSet<Double>();
val.add(1.0);
val.add(2.0);
val.add(11.0);
val.add(12.0);
ArrayList<String> arr = new ArrayList<String>();
double posinf = Double.POSITIVE_INFINITY;
double neginf = Double.NEGATIVE_INFINITY;
arr.add(neginf+ " - " +val.first());
Iterator<Double> it = val.iterator();
while (it.hasNext()) {
// Get element
Object lowerBound = it.next();
Object upperBound = it.next();
arr.add(lowerBound+" - "+upperBound);
}
arr.add(val.last() + " - "+ posinf);
System.out.println("Range array: "+arr);
}
}
我当前的输出是:
Range array: [-Infinity - 1.0, 1.0 - 2.0, 11.0 - 12.0, 12.0 - Infinity]
我希望范围数组为:
[-Infinity - 1.0, 1.0 - 2.0, 2.0 - 11.0, 11.0 - 12.0, 12.0 - Infinity]
您在循环的每次迭代中都使用了两个元素(如果元素数量为奇数,则会引发异常)。您应该在每次迭代中只消耗一个:
Iterator<Double> it = val.iterator();
Double lowerBound = neginf;
while (it.hasNext()) {
// Get element
Double upperBound = it.next();
arr.add(lowerBound+" - "+upperBound);
lowerBound = upperBound;
}
arr.add(lowerBound + " - "+ posinf);
每个 it.next() 调用都会使迭代器前进一步。因此,在 while 循环的每次迭代中,您将失去一个间隔。使用一个临时变量来保存之前的迭代器值。
比如
Iterator<Double> it = val.iterator();
Object end=null;
if(it.hasNext()){
end= it.next();
//write out -infinity to previous.
}
while(it.hasNext()){
Object start = end;
end= it.next();
//write out start - end interval
}
if(end != null){
// write out end to infinity
} else {
// there were no values in the array.
// write out - infinity to infinity?
}
问题出在以下循环
while (it.hasNext()) {
// Get element
Object lowerBound = it.next();
Object upperBound = it.next();
arr.add(lowerBound+" - "+upperBound);
}
迭代器 it
在单次迭代中递增两次 it.next()
,最终生成您正在获取的数组。
解决方法如下:
Double lowerBound = neginf;
while (it.hasNext()) {
// Get element
Double upperBound = it.next();
arr.add(lowerBound + " - "+ upperBound);
lowerBound = upperBound;
}
我正在尝试为存储在 SortedSet 中的双精度值创建间隔。
下面是我的代码:
public class Trail {
public static void main(String[] args) {
SortedSet<Double> val = new TreeSet<Double>();
val.add(1.0);
val.add(2.0);
val.add(11.0);
val.add(12.0);
ArrayList<String> arr = new ArrayList<String>();
double posinf = Double.POSITIVE_INFINITY;
double neginf = Double.NEGATIVE_INFINITY;
arr.add(neginf+ " - " +val.first());
Iterator<Double> it = val.iterator();
while (it.hasNext()) {
// Get element
Object lowerBound = it.next();
Object upperBound = it.next();
arr.add(lowerBound+" - "+upperBound);
}
arr.add(val.last() + " - "+ posinf);
System.out.println("Range array: "+arr);
}
}
我当前的输出是:
Range array: [-Infinity - 1.0, 1.0 - 2.0, 11.0 - 12.0, 12.0 - Infinity]
我希望范围数组为:
[-Infinity - 1.0, 1.0 - 2.0, 2.0 - 11.0, 11.0 - 12.0, 12.0 - Infinity]
您在循环的每次迭代中都使用了两个元素(如果元素数量为奇数,则会引发异常)。您应该在每次迭代中只消耗一个:
Iterator<Double> it = val.iterator();
Double lowerBound = neginf;
while (it.hasNext()) {
// Get element
Double upperBound = it.next();
arr.add(lowerBound+" - "+upperBound);
lowerBound = upperBound;
}
arr.add(lowerBound + " - "+ posinf);
每个 it.next() 调用都会使迭代器前进一步。因此,在 while 循环的每次迭代中,您将失去一个间隔。使用一个临时变量来保存之前的迭代器值。
比如
Iterator<Double> it = val.iterator();
Object end=null;
if(it.hasNext()){
end= it.next();
//write out -infinity to previous.
}
while(it.hasNext()){
Object start = end;
end= it.next();
//write out start - end interval
}
if(end != null){
// write out end to infinity
} else {
// there were no values in the array.
// write out - infinity to infinity?
}
问题出在以下循环
while (it.hasNext()) {
// Get element
Object lowerBound = it.next();
Object upperBound = it.next();
arr.add(lowerBound+" - "+upperBound);
}
迭代器 it
在单次迭代中递增两次 it.next()
,最终生成您正在获取的数组。
解决方法如下:
Double lowerBound = neginf;
while (it.hasNext()) {
// Get element
Double upperBound = it.next();
arr.add(lowerBound + " - "+ upperBound);
lowerBound = upperBound;
}