- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
how to shuffle a 2D array in java correctly?
Yes. Create a list to represent a 2D array and then use Collections.shuffle(list).
Example
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Tester { public static void main(String[] args) { List<int[]> rows = new ArrayList<>(); rows.add(new int[]{1,2,3}); rows.add(new int[]{4,5,6}); rows.add(new int[]{7,8,9}); System.out.println("Before Shuffle"); System.out.println("[0][0] : " + rows.get(0)[0]); System.out.println("[1][1] : " + rows.get(1)[1]); System.out.println("After Shuffle"); Collections.shuffle(rows); System.out.println("[0][0] : " + rows.get(0)[0]); System.out.println("[1][1] : " + rows.get(1)[1]); } }
Output
Before Shuffle [0][0] : 1 [1][1] : 5 After Shuffle [0][0] : 7 [1][1] : 2
- Related Articles
- How to store a 2d Array in another 2d Array in java?
- How to shuffle an array in Java?
- How to create a dynamic 2D array in Java?
- How to randomize and shuffle array of numbers in Java?
- How to shuffle a List in Java
- How to randomize (shuffle) a JavaScript array?
- How to read a 2d array from a file in java?
- Java Program to shuffle an array using list
- How to shuffle an array in a random manner in JavaScript?
- Print a 2D Array or Matrix in Java
- How to sort a 2D array in TypeScript?
- How to get rows and columns of 2D array in Java?
- Shuffle Array Contents
- How to convert a 2D array into 1D array in C#?
- How to populate a 2d array with random alphabetic values from a range in Java?

Advertisements