1

Is there any way to fill two dimensional array with unique random number ? I tried so much but all of my tries are failed . I can do this

ArrayList<Integer> list = new ArrayList<>();
        
        for (int i = 1; i < 26; i++) {         //element will be in range (1,25)
            list.add(i);
        }
        Collections.shuffle(list);
        for (int j = 0; j < 5; j++) {
            System.out.print(list.get(j) + "     ");
        }
        System.out.println();

4 Answers 4

1

If you wanted to print a 5x5 matrix of numbers from the List, you just need two layers of for loops. See the below code in action here.

ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i < 26; i++) { // element will be in range (1,25)
    list.add(i);
}
Collections.shuffle(list);
for (int j = 0; j < 5; j++) {
    for (int k = 0; k < 5; k++) {
        System.out.format("%3d     ", list.get(j * 5 + k));
    }
    System.out.println();
}
System.out.println();

Example Output:

  3       4      23      18      15     
  1       8      20       6       7     
  5      21      19       2      24     
 17      13      22      16      25     
 14       9      12      10      11 
Sign up to request clarification or add additional context in comments.

Comments

1

I guess you can use combination of library which generates the random number and hashset. Hashset to remember the random number generated so far, and if duplicate is generated, you re-generate until it gives you the unseen number

Comments

1

Try this.

static List<List<Integer>> uniqueRandomNumbers(int height, int width) {
    List<Integer> list = IntStream.rangeClosed(1, height * width)
        .boxed()
        .collect(Collectors.toList());
    Collections.shuffle(list);
    List<List<Integer>> matrix = IntStream.range(0, height)
        .mapToObj(i -> list.subList(i * width, (i + 1) * width))
        .collect(Collectors.toList());
    return matrix;
}

and

List<List<Integer>> matrix = uniqueRandomNumbers(5, 5);
for (List<Integer> list : matrix)
    System.out.println(list);

result

[16, 4, 15, 14, 25]
[19, 11, 6, 21, 9]
[17, 20, 3, 1, 5]
[10, 7, 22, 18, 2]
[12, 13, 24, 23, 8]

Comments

0

Does this can help ?

public static void main(String[] args)
   {
      // declare arrays
      int[][] ticketInfo;
      String[][] seatingChart;

      // create arrays
      ticketInfo = new int [2][3];
      seatingChart =  new String [3][2];

      // initialize the array elements
      ticketInfo[0][0] = 15;
      ticketInfo[0][1] = 10;
      ticketInfo[0][2] = 15;
      ticketInfo[1][0] = 25;
      ticketInfo[1][1] = 20;
      ticketInfo[1][2] = 25;
      seatingChart[0][0] = "Jamal";
      seatingChart[0][1] = "Maria";
      seatingChart[1][0] = "Jacob";
      seatingChart[1][1] = "Suzy";
      seatingChart[2][0] = "Emma";
      seatingChart[2][1] = "Luke";

      // print the contents
      System.out.println(ticketInfo);
      System.out.println(seatingChart);
   }

1 Comment

it'll not work because "ticketInfo" and "seatingChart" are arrays and in printing you called them as variable and if we call them in loop it will print the values which we set it for them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.