简体   繁体   中英

How to make a dynamic 2D array and store a shuffled array in it

I've created a 2D arraylist that has a fixed number or rows and an array that holds numbers 1-4. I'm supposed to shuffle the array and then store that array in the arraylist. However, when I go to print the entire arraylist after, it doesn't match, and it appears, it is taking my last shuffle and printing it for all the rows.

For example, one of my outputs is:

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

Can someone help me understand my mistake?

 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]); } } 

When you are adding your teamNums to teamMatches, you are passing the reference (pointer) to the same array (same memory location). Therefore when you print after the for loop you will only get the last known shuffle because that's what the array looks like.

You must declare a new array variable for each iteration of the for loop. Try :

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]); 
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM