<legend id='8LIj7'><style id='8LIj7'><dir id='8LIj7'><q id='8LIj7'></q></dir></style></legend>
    <bdo id='8LIj7'></bdo><ul id='8LIj7'></ul>
  • <tfoot id='8LIj7'></tfoot>

      <small id='8LIj7'></small><noframes id='8LIj7'>

    1. <i id='8LIj7'><tr id='8LIj7'><dt id='8LIj7'><q id='8LIj7'><span id='8LIj7'><b id='8LIj7'><form id='8LIj7'><ins id='8LIj7'></ins><ul id='8LIj7'></ul><sub id='8LIj7'></sub></form><legend id='8LIj7'></legend><bdo id='8LIj7'><pre id='8LIj7'><center id='8LIj7'></center></pre></bdo></b><th id='8LIj7'></th></span></q></dt></tr></i><div id='8LIj7'><tfoot id='8LIj7'></tfoot><dl id='8LIj7'><fieldset id='8LIj7'></fieldset></dl></div>

      1. 如何在给定范围内创建一个随机打乱数字的 int

        时间:2023-10-02
          <tbody id='RoRus'></tbody>

            <small id='RoRus'></small><noframes id='RoRus'>

          • <i id='RoRus'><tr id='RoRus'><dt id='RoRus'><q id='RoRus'><span id='RoRus'><b id='RoRus'><form id='RoRus'><ins id='RoRus'></ins><ul id='RoRus'></ul><sub id='RoRus'></sub></form><legend id='RoRus'></legend><bdo id='RoRus'><pre id='RoRus'><center id='RoRus'></center></pre></bdo></b><th id='RoRus'></th></span></q></dt></tr></i><div id='RoRus'><tfoot id='RoRus'></tfoot><dl id='RoRus'><fieldset id='RoRus'></fieldset></dl></div>

                  <bdo id='RoRus'></bdo><ul id='RoRus'></ul>
                • <tfoot id='RoRus'></tfoot>
                • <legend id='RoRus'><style id='RoRus'><dir id='RoRus'><q id='RoRus'></q></dir></style></legend>

                  本文介绍了如何在给定范围内创建一个随机打乱数字的 int 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  基本上,假设我有一个可以容纳 10 个数字的 int 数组.这意味着我可以在每个索引中存储 0-9(每个数字只能存储一次).

                  Basically, let's say I have an int array that can hold 10 numbers. Which mean I can store 0-9 in each of the index (each number only once).

                  如果我运行下面的代码:

                  If I run the code below:

                  int[] num = new int[10];
                  for(int i=0;i<10;i++){
                      num[i]=i;
                  }
                  

                  我的数组看起来像这样:

                  my array would look like this:

                  [0],[1],.....,[8],[9]
                  

                  但是如何在每次运行代码时随机分配数字?例如,我希望数组看起来像:

                  But how do I randomize the number assignment each time I run the code? For example, I want the array to look something like:

                  [8],[1],[0].....[6],[3]
                  

                  推荐答案

                  将其设为 List 而不是数组,并使用 Collections.shuffle() 对其进行随机播放.您可以在洗牌后从列表中构建 int[].

                  Make it a List<Integer> instead of an array, and use Collections.shuffle() to shuffle it. You can build the int[] from the List after shuffling.

                  如果你真的想直接进行随机播放,请搜索Fisher-Yates Shuffle".

                  If you really want to do the shuffle directly, search for "Fisher-Yates Shuffle".

                  以下是使用列表技术的示例:

                  Here is an example of using the List technique:

                  import java.util.ArrayList;
                  import java.util.Collections;
                  import java.util.List;
                  
                  public class Test {
                    public static void main(String args[]) {
                      List<Integer> dataList = new ArrayList<Integer>();
                      for (int i = 0; i < 10; i++) {
                        dataList.add(i);
                      }
                      Collections.shuffle(dataList);
                      int[] num = new int[dataList.size()];
                      for (int i = 0; i < dataList.size(); i++) {
                        num[i] = dataList.get(i);
                      }
                  
                      for (int i = 0; i < num.length; i++) {
                        System.out.println(num[i]);
                      }
                    }
                  }
                  

                  这篇关于如何在给定范围内创建一个随机打乱数字的 int 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:java的行为不一致== 下一篇:如何将整数转换为整数?

                  相关文章

                  最新文章

                  <small id='ZRhdH'></small><noframes id='ZRhdH'>

                    • <bdo id='ZRhdH'></bdo><ul id='ZRhdH'></ul>

                    1. <i id='ZRhdH'><tr id='ZRhdH'><dt id='ZRhdH'><q id='ZRhdH'><span id='ZRhdH'><b id='ZRhdH'><form id='ZRhdH'><ins id='ZRhdH'></ins><ul id='ZRhdH'></ul><sub id='ZRhdH'></sub></form><legend id='ZRhdH'></legend><bdo id='ZRhdH'><pre id='ZRhdH'><center id='ZRhdH'></center></pre></bdo></b><th id='ZRhdH'></th></span></q></dt></tr></i><div id='ZRhdH'><tfoot id='ZRhdH'></tfoot><dl id='ZRhdH'><fieldset id='ZRhdH'></fieldset></dl></div>

                      <legend id='ZRhdH'><style id='ZRhdH'><dir id='ZRhdH'><q id='ZRhdH'></q></dir></style></legend>
                      <tfoot id='ZRhdH'></tfoot>