首页 \ 问答 \ 随机化数字1到100的数组(Randomize Array of numbers 1 through 100)

随机化数字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
更新时间:2022-12-28 07:12

最满意答案

通常不应在迭代时修改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 fine

Thanks 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()不检查并发修改,只是检查我 ...
  • 异常堆栈跟踪指向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 ...
  • 我能够使用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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。