随机化数字1到100的数组(Randomize Array of numbers 1 through 100)
我正在尝试创建一个程序来创建数字1到100的数组,然后将它们随机化。 到目前为止我有这个,但不知道接下来该做什么:
public class Random100Array { public static void main(String args[]) { { int[] nums = new int[100]; char current; int a; for (int i = 0; i <= nums.length; i++) { nums[i] = i + 1; } for (int i1 = 0; i1 <=nums.length; i1++) { double random = (Math.random() * 100) + 1; } } } }
此外,这不是家庭作业。 我是学生,我目前正在寒假里。 这个程序出于某种原因给我这个输出。 我究竟做错了什么?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 at Random100Array.main(Random100Array.java:11)
I'm trying to create a program to create an array of the numbers 1 through 100 and then randomize them. So far I have this, but don't know what to do next:
public class Random100Array { public static void main(String args[]) { { int[] nums = new int[100]; char current; int a; for (int i = 0; i <= nums.length; i++) { nums[i] = i + 1; } for (int i1 = 0; i1 <=nums.length; i1++) { double random = (Math.random() * 100) + 1; } } } }
Also, THIS ISN'T HOMEWORK. I am a student and I'm currently on winter break. This program gives me this output for some reason. What am I doing wrong?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 at Random100Array.main(Random100Array.java:11)
原文:https://stackoverflow.com/questions/20873435
最满意答案
通常不应在迭代时修改
Collection
。 修改元素很好,但是在迭代时你真的不应该从Collection
删除某些东西。 请参见此处: http : //docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 。 此外,ConcurrentModificationException
的Javadoc可能会有所帮助。您可以尝试返回删除了
Supplement
的新列表:private static List<Supplement> removeSup(Supplement supToRemove, List<Supplement> listToRemoveFrom) { List<Supplement> filteredSupplements = new ArrayList<Supplement>(); for(Supplement supplement : listToRemoveFrom) { if(!suppplement.equals(supToRemove)){ filteredSupplements.add(supplement); } } return filteredSupplements; }
I figured out that the search i was doing was not working with what i wanted to do so i created a method which returns an integer of the Supplement in the list.
private static int indexOfSup(List<Supplement> supSearchList, String nameOfSup) { for (Supplement sup : supSearchList) { if (sup.getSupName().equalsIgnoreCase(nameOfSup)) { return supSearchList.indexOf(sup); } } return -1; }
i then use this integer to remove from the list. a simple
List.Remove(index)
worked fineThanks for all the replies.
相关问答
更多-
解决此问题的最简单方法是不将负索引传递给list.remove() 。 发生第二个异常的原因是remove执行部分范围检查,仅检查传递的index是否太大。 它不会检查它是否为负数,因为在这种情况下它依赖于支持数组的访问来抛出异常。 因此,对于负索引,在抛出ArrayIndexOutOfBoundsException之前, ArrayList的modCount会递增。 因此,尝试在捕获此异常后继续遍历列表会抛出ConcurrentModificationException 。 public E remove ...
-
来自代码 for (Project persist: projectList) { persist.setProjectId("K7890"); persist.setName(fileName); myDAO.saveProjects(projectList); } projectList.clear(); // <-- clear might cause to this Exception 参考 为什么在使用迭代器时会遇到ConcurrentModificat ...
-
首先要知道的是(如JLS中所述)以下增强的for循环: for (String s : list) { // Do something with s } 相当于: for (Iterator
it = list.iterator(); it.hasNext();) { String s = it.next(); // Do something with s } 如果你看看AbstractList迭代器的实现,你会看到: hasNext()不检查并发修改,只是检查我 ... -
错误:java.util.ConcurrentModificationException(Error : java.util.ConcurrentModificationException)[2022-07-11]
异常堆栈跟踪指向jsp中的s:iterator ,它是抛出异常的地方。 这意味着当该元素通过书籍列表时,另一段代码会从列表中添加或删除。 这可能是您的Java代码或其他一些代码(例如RemovebooksFromSession )。 查看您的代码,并尝试确定在构建jsp页面时是否可能运行其他代码。 使用您发布的代码无法帮助您。 The exception stack trace points to the s:iterator in your jsp being the place where the ex ... -
一旦你创建了一个迭代器(除了通过迭代器),你就不能改变Iterable的内容,否则一旦移动迭代器就会得到一个ConcurrentModificationException - 你创建一个迭代器,然后执行noProcess.add(new Integer("5")); ,然后稍后推进迭代器。 另外,你创建了两个迭代器 - 你不应该那样做 - 这太疯狂了。 You can't alter the contents of an Iterable once you create an iterator on it ...
-
我假设你正在渲染一些东西在code.frame.GameEngine.render()方法中进行屏幕显示。 问题是你正在使用一个不是线程安全的LinkedList 。 因此,可以向elements添加一个新的GUIElement ,而code.frame.GameEngine.render()方法遍历elements (这是异步发生的)。 您需要使用线程安全的集合,如: List
elements = Collections.synchronizedList(new LinkedLi ... -
带有ConcurrentModificationException的Java线程(Java Threads with ConcurrentModificationException)[2024-03-10]
我能够使用Java实现并发列表CopyOnWriteArrayList来复制问题并修复它 这是我的主要课程 public class PrimeRunnableMain { public static void main(String[] args) { PrimeRunnable.setUpperBorder(10); PrimeRunnable primeRunnable1 = new PrimeRunnable(); PrimeRunnable ... -
从底层集合中移除元素并继续迭代的唯一安全方法是使用Iterator的remove()方法。 这将删除Iterator的next()方法返回的最后一个元素。 就你而言,看起来这需要将Iterator传递给执行修改的方法(或使其成为实例字段,如Map对象已经)。 The only safe way to remove an element from an underlying collection and continue the iteration is to use the remove() method ...
-
使用foreach循环时无法修改集合。 但是,您可以迭代Map.entrySet()并执行您需要的所有操作: public void run() { for (Iterator
> i = playerCooldowns.entrySet().iterator(); i.hasNext();) { Map.Entry entry = i.next(); entry.setValue(en ... -
通常不应在迭代时修改Collection 。 修改元素很好,但是在迭代时你真的不应该从Collection删除某些东西。 请参见此处: http : //docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 。 此外, ConcurrentModificationException的Javadoc可能会有所帮助。 您可以尝试返回删除了Supplement的新列表: private static List
removeS ...