源程序:
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Arrays;
import java.util.Iterator;
public class SortedSetDemo {
public static void main(String[] args) {
SortedSet sortedSet = new TreeSet(Arrays.asList("one two three four five six seven eight".split(" ")));
System.out.println(sortedSet);
Object low = sortedSet.first();
Object high = sortedSet.last();
System.out.println(low);
System.out.println(high);
Iterator it = sortedSet.iterator();
for(int i = 0; i < 7; i++) {
if(3 == i)
low = it.next();
if(6 == i)
high = it.next();
else
it.next();
}
System.out.println(low);
System.out.println(high);
System.out.println(sortedSet.subSet(low, high));
System.out.println(sortedSet.headSet(high));
System.out.println(sortedSet.tailSet(low));
}
}
结果却是:
[eight, five, four, one, seven, six, three, two]
eight
two
one
two
[one, seven, six, three]
[eight, five, four, one, seven, six, three]
[one, seven, six, three, two]
Press any key to continue...
6 == i 怎么会打印出第8号内容呢?
我修改了一下程序,想看看i = 4,5的时候发生了什么,
源程序:
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Arrays;
import java.util.Iterator;
public class SortedSetDemo {
public static void main(String[] args) {
SortedSet sortedSet = new TreeSet(Arrays.asList("one two three four five six seven eight".split(" ")));
System.out.println(sortedSet);
Object low = sortedSet.first();
Object high = sortedSet.last();
System.out.println(low);
System.out.println(high);
Iterator it = sortedSet.iterator();
for(int i = 0; i < 7; i++) {
if(3 == i)
low = it.next();
if(4 == i) {
System.out.println("4");
it.next();
}
if(5 == i) {
System.out.println("5");
it.next();
}
if(6 == i)
high = it.next();
else
it.next();
}
System.out.println(low);
System.out.println(high);
System.out.println(sortedSet.subSet(low, high));
System.out.println(sortedSet.headSet(high));
System.out.println(sortedSet.tailSet(low));
}
}
结果却变成了:
[eight, five, four, one, seven, six, three, two]
eight
two
4
5
Exception in thread "main" java.util.NoSuchElementException
at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1029)
at java.util.TreeMap$KeyIterator.next(TreeMap.java:1058)
at SortedSetDemo.main(SortedSetDemo.java:29)
Press any key to continue...
整个一报错了。
我看了好久都百思不得其解~~~~
哪位来帮帮偶啊~~~~~~~~~~~~~~~~~~~~~
困扰好久好久了。。。。。。。。