Tag: 迭代

迭代Map 时出现ConcurrentModificationException

我在下面有以下代码 Map buyingItemEnumerationMap = this.toBuyItemEnumeration; for (Entry item : buyingItemEnumerationMap.entrySet()) { if(RandomEngine.boolChance(50)){ //will delete? buyingItemEnumerationMap.remove(item.getKey()); } if(buyingItemEnumerationMap.size() == 1){ break; } } 现在我正在使用Android游戏,上面的代码以multithreading方式运行。 现在我遇到了一个exception,即java.util.ConcurrentModificationException 。 我已经研究过如何解决这个问题,但似乎没有对我有所帮助。 我在上面的代码上做的是随机删除一个条目 。 我怎样才能在那里实现它?

Jsoup – 提取文本

我需要从这样的节点中提取文本: Some text with tags might go here. Also there are paragraphs More text can go without paragraphs 我需要建立: Some text with tags might go here. Also there are paragraphs More text can go without paragraphs Element.text只返回div的所有内容。 Element.ownText – 不在children元素中的所有内容。 两者都错了。 通过children迭代忽略文本节点。 是否有方法迭代元素的内容以接收文本节点。 例如 文本节点 – 一些文本 节点 – 带标签 文本节点 – 可能会在这里。 节点 – […]

如何在没有迭代的情况下获取给定LinkedHashSet元素的索引?

它甚至可能吗? 说你有 private Set names = new LinkedHashSet(); Strings是“迈克”,“约翰”,“凯伦”。 是否有可能在没有迭代的情况下得到“1”以回答“约翰”的索引是什么? 以下工作正常..有了这个问题,我想知道是否有更好的方法 for (String s : names) { ++i; if (s.equals(someRandomInputString)) { break; } }

递归与迭代(Fibonacci序列)

我有两种不同的方法,一种是使用迭代计算第n个元素的Fibonacci序列,另一种是使用递归方法做同样的事情。 程序示例如下所示: import java.util.Scanner; public class recursionVsIteration { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //nth element input System.out.print(“Enter the last element of Fibonacci sequence: “); int n = sc.nextInt(); //Print out iteration method System.out.println(“Fibonacci iteration:”); long start = System.currentTimeMillis(); System.out.printf(“Fibonacci sequence(element at index %d) = %d \n”, n, fibIteration(n)); System.out.printf(“Time: […]

迭代可变长度的Java数组

如何迭代可变长度的Java数组。 我想我会设置一个while循环,但是如何检测到我已到达数组的末尾。 我想我想要这样的东西[只需要弄清楚如何表示myArray.notEndofArray()] index = 0; while(myArray.notEndofArray()){ system.out.println(myArray(index)); index++; }

JSTL迭代对象列表

我在jsp中获得了一个对象列表’myList’。 我得到的对象属于’MyClass’。 我想通过JSTL迭代这个列表。 JSP代码如下: ${element.getStatus()} ${element.getRequestType()} ${element.getRequestedFor()} ${element.getTimeSubmitted()} 我得到例外: 00000024 WebApp E [Servlet Error]-[/requestHistory.jsp]: com.ibm.ws.jsp.translator.JspTranslationException: JSPG0227E: Exception caught while translating /requestHistory.jsp: /requestHistory.jsp(31,6) –> JSPG0122E: Unable to parse EL function ${UserProcessRequests.getStatus()}. 我正在使用的Taglib是:

如何避免使用大型if语句和instanceof

动物 public abstract class Animal { String name; public Animal(String name) { this.name = name; } } 狮子 public class Lion extends Animal { public Lion(String name) { super(name); // TODO Auto-generated constructor stub } public void roar() { System.out.println(“Roar”); } } 鹿 public class Deer extends Animal { public Deer(String name) { super(name); } […]

如何有效地迭代Java Map中的每个条目?

如果我有一个用Java实现Map接口的对象,并希望迭代其中包含的每一对,那么通过地图的最有效方法是什么? 元素的排序是否取决于我对界面的具体映射实现?