| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 927 人关注过本帖
标题:我真的要疯了!真心求各位看看这个是为什么?
只看楼主 加入收藏
believe45
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2005-12-7
收藏
 问题点数:0 回复次数:6 
我真的要疯了!真心求各位看看这个是为什么?

源程序:
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...

整个一报错了。
我看了好久都百思不得其解~~~~
哪位来帮帮偶啊~~~~~~~~~~~~~~~~~~~~~
困扰好久好久了。。。。。。。。

搜索更多相关主题的帖子: 真心 
2006-07-26 21:57
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
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();
else if(4 == i) {
System.out.println("4");
it.next();
}
else if(5 == i) {
System.out.println("5");
it.next();
}
else 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));
}
}
你知道你的程序为什么会错吗?
就是因为你的if....else没有用好
使得你的else语句每次都会执 行,只有当i=6的时候才不会执行
要改很容易,加几个else就可以了

可惜不是你,陪我到最后
2006-07-27 11:17
believe45
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2005-12-7
收藏
得分:0 
哦!!!!!!!!!!!!!!!!!!!!!
原来是这样~~~~~~~~~~~~~
终于搞懂了~~~~~~~~~~~~~
谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢!!!!!
:)
再次感谢~
呵呵
斑竹好人啊~~~~
2006-07-27 11:59
丛林特特
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-1-18
收藏
得分:0 
SortedSet sortedSet = new TreeSet(Arrays.asList("one two three four five six seven eight".split(" ")));
System.out.println(sortedSet);
麻烦问一下为什么最后的输出会是:[eight, five, four, one, seven, six, three, two]

谢谢!
2006-07-31 14:46
believe45
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2005-12-7
收藏
得分:0 
呵呵,按字典序排的么。。。。
2006-07-31 15:39
丛林特特
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-1-18
收藏
得分:0 
TreeSet(Arrays.asList)("".split(""))
是这个代码代表字典排吗??
谢谢!
2006-07-31 17:37
丛林特特
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-1-18
收藏
得分:0 
TreeSet(Arrays.asList)("".split(""))
是这个代码代表字典排吗??
谢谢!
2006-07-31 17:37
快速回复:我真的要疯了!真心求各位看看这个是为什么?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018411 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved