Tag: 迭代器

为什么Iterator接口中没有add方法

在Iterator Sun添加了remove方法来删除集合中最后访问的元素。 为什么没有添加方法来向集合中添加新元素? 它可能对集合或迭代器有什么样的副作用?

如何在数组列表中连接字符串值

我需要使用concat一次打印所有的arraylist值。 这是我的代码: ArrayList lst = new ArrayList(); lst.add(“hi”); lst.add(“hello”); Iterator itr = lst.iterator(); String result = null; while(itr.hasNext()) { Object element = itr.next(); result = element + ” “; } System.out.println(result); 预期的结果应该是hi hello 。 然而,当前输出是hello (最后还有一个空格 )。

使用Iterator的ConcurrentModificationException

我正在使用迭代器循环集合,如下所示: Iterator entityItr = entityList.iterator(); while (entityItr.hasNext()) { Entity curr = entityItr.next(); for (Component c : curr.getComponents()) { if (c instanceof PlayerControlled) { ((PlayerControlled) c).pollKeyboard(); } } } 但是在下一行中,我得到一个ConcurrentModificationException Entity curr = entityItr.next(); 当我没有改变什么时,为什么会这样? 非常感谢 编辑 – 堆栈跟踪: java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at cw.systems.Input.checkInputs(Input.java:31) at cw.systems.Input.begin(Input.java:21) at cw.misc.Game.render(Game.java:73) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

Java:Interleave两个基于整数的arraylists – >好方法?

家庭作业:寻找更好的策略,或者接近而不是完整的代码。 我在两个条件下有两个整数数组列表: 第一个列表大于第二个列表 第二个列表大于第一个列表 我的目标是在两种条件下将list2的元素交织到list1中。 我已经创建了一个方法来做到这一点,但我觉得我可以做得更好。 这是条件1的预期结果。请注意,在list2的元素用完之后,我们将list1的元素保留在原位: list1: [10, 20, 30, 40, 50, 60, 70] list2: [4, 5, 6, 7] Combined: [10, 4, 20, 5, 30, 6, 40, 7, 50, 60, 70] 这是条件2的预期结果。由于list2有更多元素,因此在list1耗尽后,我们将这些元素附加到list1: list1: [10, 20, 30, 40] list2: [4, 5, 6, 7, 8, 9, 10, 11] Combined: [10, 4, 20, 5, 30, 6, 40, […]

迭代器上的next()方法如何工作?

我对迭代器上的next()方法有疑问。 如果我作为我的代码的一部分这行(使用arrayOfStrings size = 4): Iterator it = arrayOfStrings.iterator(); //arrayOfString is ArrayList while(it.hasNext()) { String e = it.next(); System.out.println(e); } 在第一次迭代中,迭代器开始指向索引为0的元素? 还是喜欢“索引-1”? 我问,因为据我所知next()方法返回集合中的下一个元素。 因此,如果在第一次迭代时,迭代器在调用next()时从索引0开始,它将返回索引1处的元素,并且我将无法对索引0处的元素执行任何操作?

删除java arraylist中的重复项

谢谢Marko。 我重写了代码。 尽量使它变得简单。 这次它真的可以编译。 但它只能删除重复的项目彼此相邻。 例如,如果我输入1 2 3 3 4 4 5 1 – 输出为1 2 3 4 5 1.它最终无法获取副本。 (顺便说一句:这个网站的新手,如果让任何显示混乱我的道歉) 这是新代码: import java.util.*; public class SetListDemo{ public static void main(String[] args){ SetListType newList = new SetListType(); Scanner keyboard = new Scanner(System.in); System.out.println( “Enter a series of items: “); String input = keyboard.nextLine(); String[] original […]

迭代列表,但它只显示最后一项

我想要实现的是有一个JLabel来显示List多个项目。 我已经将列表定义如下,但是当我测试代码时,我的方法迭代遍历列表,单击按钮后,只显示列表中的最后一项 “DONE!” 。 我试图在每个按钮单击一个JLabel后, 只显示列表中的下一个项目 。 public class ScoutGUI extends javax.swing.JFrame { /** * Creates new form ScoutGUI */ List strings = Arrays.asList(“Do you mind Clutter in Room?”, “Do you mind alarm clocks?”,”Do you mind loud visitors?”,”Can you sleep with lights on?”,”Do you mind noise past Midnight?”, “Do you consider yourself as an introvert?”, […]

Java中的迭代器

什么是迭代器和集合? 这两个有关系吗? // the interface definition Interface Iterator { boolean hasNext(); Object next(); // note “one-way” traffic void remove(); } // an example public static void main (String[] args){ ArrayList cars = new ArrayList(); for (int i = 0; i < 12; i++) cars.add (new Car()); Iterator it = cats.iterator(); while (it.hasNext()) System.out.println ((Car)it.next()); } […]

在一个ArrayList中使用两个迭代器

编辑:感谢您的所有提示回复。 现在我看到作业不起作用。 从另一个线程我读到,Java中的迭代器比C ++中的迭代器强大得多。 请问为什么在Java中使用iterator? 只是为了取代“for”循环? 谢谢。 一些说明: 第二个迭代器应该从第一个迭代器的位置开始。 我尝试从头开始查看有序列表,在列表中找到一些具有与aItr指向的类似属性的对象。 我不介意使用两个“for”循环,但我知道Java对所有这些库都非常强大。 我只是好奇是否有比两个“for”循环更好的方法。 谢谢。 嗨, 我一直在使用C ++,但我是Java的新手,所以请耐心等待。 我尝试使用两个迭代器遍历ArrayList。 第一个迭代器遍历列表,第二个迭代器从第一个迭代器指向的位置开始,一直到列表的末尾。 以下代码是我想要做的(虽然可能无效): …….; //initialize aList here …… Iterator aItr = aList.iterator(); while(aItr.hasNext()){ int a = aItr.next(); Iterator bItr = aItr; //—–> is it valid? Any bad consequence? while (bItr.hasNext()){ …………; //do stuff } } 将一个迭代器分配给另一个迭代器是否有效? 如果没有,那么做我想做的最好的方法是什么? 谢谢。 我知道它在C […]

HashMap:以随机顺序迭代键值对

我有一个HashMap,我想在每次获得迭代器时以不同的随机顺序迭代它们的键值对。 从概念上讲,我想在调用迭代器之前“洗牌”地图(或者如果你想要,“洗牌”迭代器)。 我有两种选择: 1)使用LinkedHashMap的方法并在内部保留条目列表,将其随地移动并在调用迭代器时返回该视图。 2)取map.entrySet(),构造一个ArrayList并在其上使用shuffle()。 虽然这两种方法看起来很像我,但我期待非常大的HashMaps,所以我真的很关心细节和内部,因为我真的不能浪费内存或计算。