【问题标题】:How to make a dynamic 2D array and store a shuffled array in it如何制作动态二维数组并在其中存储混洗数组
【发布时间】:2019-04-20 05:29:02
【问题描述】:

我创建了一个二维数组列表,它有一个或多个固定行数和一个包含数字 1-4 的数组。我应该对数组进行洗牌,然后将该数组存储在数组列表中。但是,当我之后打印整个数组列表时,它不匹配,并且它出现了,它正在使用我的最后一次随机播放并为所有行打印它。

例如,我的输出之一是:

3、2、1、4

1、2、4、3

2、1、3、4

2、3、4、1


2、3、4、1

2、3、4、1

2、3、4、1

2、3、4、1

谁能帮我理解我的错误?

package practice;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class Practice {
  public static void main(String[] args) {
    //Make arraylist for teams
    List < Integer[] > teamMatches = new ArrayList < > ();
    //Array for team numbers
    Integer[] teamNums = new Integer[] {
      1,
      2,
      3,
      4
    };

    for (int i = 0; i < 4; i++) {
      //shuffle array    
      Collections.shuffle(Arrays.asList(teamNums));
      //add array to arraylist
      teamMatches.add(teamNums);
      //print out
      System.out.println(teamMatches.get(i)[0] + ", " + teamMatches.get(i)[1] + ", " +
        teamMatches.get(i)[2] + ", " + teamMatches.get(i)[3]);

    }
    System.out.println("_____________________________");
    //print out entire match array
    for (int n = 0; n < 4; n++) {

      System.out.println(teamMatches.get(n)[0] + ", " + teamMatches.get(n)[1] + ", " +
        teamMatches.get(n)[2] + ", " + teamMatches.get(n)[3]);




    }



  }

【问题讨论】:

  • 你只是想打乱array的数组还是每个数组中的元素???预期的结果是什么?
  • 我只想打乱 teamNums 数组。之后,我想将洗牌后的数组放入 teamMatches 数组列表中。我的预期结果是第一个循环打印出一个 4x4 数组,每行包含随机顺序的数字 1-4,没有重复。第二个循环只是用来检查洗牌后的数组是否被正确存储。如果两个打印的数组匹配,那么我知道我正在以正确的顺序存储我的洗牌数组

标签: java arrays multidimensional-array dynamic shuffle


【解决方案1】:

当您将 teamNums 添加到 teamMatches 时,您将引用(指针)传递给相同的数组(相同的内存位置)。因此,当您在 for 循环之后打印时,您只会得到最后一个已知的 shuffle,因为这就是数组的样子。

您必须为 for 循环的每次迭代声明一个新的数组变量。 试试看:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class Practice {
    public static void main(String[] args) {
        //Make arraylist for teams
        List < Integer[] > teamMatches = new ArrayList < > ();

        for (int i = 0; i < 4; i++) {
            // *create new Array for team numbers
            Integer[] teamNums = new Integer[] {1, 2, 3, 4};

            //shuffle array    
            Collections.shuffle(Arrays.asList(teamNums));

            //add array to arraylist
            teamMatches.add(teamNums);

            //print out
            System.out.println(
                teamMatches.get(i)[0] + ", " 
                + teamMatches.get(i)[1] + ", "
                + teamMatches.get(i)[2] + ", "
                + teamMatches.get(i)[3]
            );
        }
        System.out.println("_____________________________");

        //print out entire match array
        for (int n = 0; n < 4; n++) {    
            System.out.println(
                teamMatches.get(n)[0] + ", "
                + teamMatches.get(n)[1] + ", "
                + teamMatches.get(n)[2] + ", "
                + teamMatches.get(n)[3]); 
        }
    }
}

【讨论】:

  • 好的,谢谢!这解决了我的问题。只是为了我的理解/澄清,之前,我基本上只是在循环之后打印出最后一个 shuffle,因为这是唯一可以引用的东西?
  • 是的。您实际上是在将相同的数组(相同的内存位置)添加到 teamMatches。因此,虽然 for 循环的每次迭代都在对数组进行洗牌,但当你在 for 循环之后打印出数组时,你只打印出最后一次洗牌,因为你之前的所有洗牌都被覆盖了。如果有帮助,请为答案投票。
  • 我投了赞成票,但由于我没有足够的声誉,它不会公开显示。谢谢大家的帮助!
猜你喜欢
  • 2012-02-02
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 2019-02-07
  • 2021-12-09
  • 2019-02-19
相关资源
最近更新 更多