<small id='3Tuvb'></small><noframes id='3Tuvb'>

    • <bdo id='3Tuvb'></bdo><ul id='3Tuvb'></ul>
  1. <legend id='3Tuvb'><style id='3Tuvb'><dir id='3Tuvb'><q id='3Tuvb'></q></dir></style></legend>
  2. <i id='3Tuvb'><tr id='3Tuvb'><dt id='3Tuvb'><q id='3Tuvb'><span id='3Tuvb'><b id='3Tuvb'><form id='3Tuvb'><ins id='3Tuvb'></ins><ul id='3Tuvb'></ul><sub id='3Tuvb'></sub></form><legend id='3Tuvb'></legend><bdo id='3Tuvb'><pre id='3Tuvb'><center id='3Tuvb'></center></pre></bdo></b><th id='3Tuvb'></th></span></q></dt></tr></i><div id='3Tuvb'><tfoot id='3Tuvb'></tfoot><dl id='3Tuvb'><fieldset id='3Tuvb'></fieldset></dl></div>
      <tfoot id='3Tuvb'></tfoot>

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

      How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)

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

      <legend id='jn3wq'><style id='jn3wq'><dir id='jn3wq'><q id='jn3wq'></q></dir></style></legend>

        1. <tfoot id='jn3wq'></tfoot>

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

              <tbody id='jn3wq'></tbody>
              • <bdo id='jn3wq'></bdo><ul id='jn3wq'></ul>
                本文介绍了如何在给定范围内创建一个随机打乱数字的 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 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

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

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

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

                        <legend id='Gg5BL'><style id='Gg5BL'><dir id='Gg5BL'><q id='Gg5BL'></q></dir></style></legend>
                          <tbody id='Gg5BL'></tbody>