BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #21384同步于 2012/2/17
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

还是迭代器的问题 新手求助

MrK
2012/2/17镜像同步16 回复
public static void main(String[] args) { LinkedList<Integer> t = new LinkedList<Integer>(); t.add(0); t.add(1); t.add(2); t.add(3); t.add(4); t.add(5); t.add(6); for(ListIterator<Integer> it = t.listIterator(); it.hasNext();) { int temp = it.next(); System.out.println("current num = "+temp); for(ListIterator<Integer> it2 = t.listIterator(it.nextIndex()); it2.hasNext(); ) { int temp2 = it2.next(); System.out.println(temp2); if(temp2 == 1) { it2.add(11111); it2.previous(); } if(temp2 == 5) { it2.remove(); } } System.out.println("current num = "+temp+" over"); } } 想要完成一个对list像起泡法那样的数据检查修改 就是循环嵌套 但是由于要修改数据 用的迭代器 结果还是没弄明白 希望高手再指点一下。。。
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
lastword机器人#1 · 2012/2/17
还是跟上次一样,it2和t都修改了,it当然会出错 【 在 MrK (Mr.K) 的大作中提到: 】 : public static void main(String[] args) : { : LinkedList<Integer> t = new LinkedList<Integer>(); : ...................
MrK机器人#2 · 2012/2/17
【 在 lastword 的大作中提到: 】 : 还是跟上次一样,it2和t都修改了,it当然会出错 恩 我知道t1还是原来的那个迭代器 没有修改插入删除 两个迭代器嵌套了 其中里面一个有修改 外面那个没有变化。。。 可是应该怎么改呢?
MrK机器人#3 · 2012/2/17
【 在 lastword 的大作中提到: 】 : 还是跟上次一样,it2和t都修改了,it当然会出错 尝试每次都改变it 但是觉得这做法很不专业。。。 请问应该怎么做比较好呢 public static void main(String[] args) { LinkedList<Integer> t = new LinkedList<Integer>(); t.add(0); t.add(1); t.add(2); t.add(3); t.add(4); t.add(5); t.add(6); for(ListIterator<Integer> it = t.listIterator(); it.hasNext();) { int temp = it.next(); System.out.println("current num = "+temp); for(ListIterator<Integer> it2 = t.listIterator(it.nextIndex()); it2.hasNext(); ) { int temp2 = it2.next(); System.out.println(temp2); if(temp2 == 1) { it2.add(11111); it2.previous(); } if(temp2 == 5) { it2.remove(); } } System.out.println("current num = "+temp+" over"); it = t.listIterator(it.nextIndex()); //加了这一句 感觉很不专业。。。 } }
GacktCamui机器人#4 · 2012/2/17
既然是排序就该直接用数组 各种数据结构都有其特性,没有最好和最差的说法 针对需求选择最合适的,不要在某一种上纠结,没有万能的完美的结构存在
MrK机器人#5 · 2012/2/17
【 在 GacktCamui 的大作中提到: 】 : 既然是排序就该直接用数组 : 各种数据结构都有其特性,没有最好和最差的说法 : 针对需求选择最合适的,不要在某一种上纠结,没有万能的完美的结构存在 不只是排序 还有对数据的修改 包括增加和删除 使用数组完成增加和删除是不是比较麻烦
lastword机器人#6 · 2012/2/17
我就觉得应该避免这种用法 干嘛非得纠结于迭代器,就你现在的需求,用一个简单的for循环不好么。。 【 在 MrK (Mr.K) 的大作中提到: 】 : 恩 我知道t1还是原来的那个迭代器 没有修改插入删除 : 两个迭代器嵌套了 其中里面一个有修改 外面那个没有变化。。。 可是应该怎么改呢?
GacktCamui机器人#7 · 2012/2/17
你这是在迭代器上钻牛角尖,能做增删和排序的容器很多 最简单的ArrayList和Vector都能做,增删和排序要分为两个步骤,在插入或者删除的时候排序是把清晰的代码写混乱的做法 要么先排序,在适当的位置插入数据 要么先插入数据,再排序 Collections.sort(list)可以排任何实现List接口的数据结构(而且是用ListIterator在排序,但不会改变ListIterator的数据),ArrayList和Vector都是 用迭代器直接增删同时排序根本就是个糟糕的选择,迭代器的最大功能是遍历数据,你这种非要用不合适的工具去解决问题的纠结是属于自寻烦恼 【 在 MrK 的大作中提到: 】 : 不只是排序 还有对数据的修改 包括增加和删除 使用数组完成增加和删除是不是比较麻烦
MrK机器人#8 · 2012/2/17
【 在 lastword 的大作中提到: 】 : 我就觉得应该避免这种用法 : 干嘛非得纠结于迭代器,就你现在的需求,用一个简单的for循环不好么。。 : 使用for 那不是又回到老问题上去了么? 在遍历的同时修改数据 会有ConcurrentModificationException 比如 for(int i=0; i<list.size; i++) { for(int j=i; j<list.size; j++) { if(list.get(j) == 1) { list.add(999); } if(list.get(j) == 2) { list.remove(j); } } }
MrK机器人#9 · 2012/2/17
【 在 GacktCamui 的大作中提到: 】 : 你这是在迭代器上钻牛角尖,能做增删和排序的容器很多 : 最简单的ArrayList和Vector都能做,增删和排序要分为两个步骤,在插入或者删除的时候排序是把清晰的代码写混乱的做法 : 要么先排序,在适当的位置插入数据 : ................... 对不起。。。。 我又没说明白。。。。 我想干的事情大体上是这样的 for(int i=0; i<list.size; i++) { for(int j=i; j<list.size; j++) { if(list.get(j) == 1) { list.add(999); } if(list.get(j) == 2) { list.remove(j); } } } 并不是排序,只是这样嵌套的遍历,同时要修改。。。。 不用迭代器的话遍历时候怎么修改(就是增加和删除)数组/ArrayList/LinkedList呀??