Kalpana
plz try to clear my doubt for shuffling multi-dimensional array
1 Answer(s)      5 years and 8 months ago
Posted in : Java Beginners

hi, if v r using Arrays.asList() means, it may shuffle the row wise list only... v want to shuffle the ful entire multi-simensional array means wat v want to do??? plz help me...

thanks in advance

Ads

View Answers

December 21, 2010 at 5:28 PM


Hi Friend,

Try the following code:

import java.util.*;
public class ShuffleMultiDimensionalArray {

  static final int size = 5;
  private int[][] array = new int[size][size];
  private List<Integer> list = new ArrayList<Integer>();

  public ShuffleMultiDimensionalArray()  {
     for (int i = 0; i < size*size; i++){
      list.add(i);
    }
    for (int i = 0; i < size; i++){
      for (int j = 0; j < size; j++){
        array[i][j] = list.get(j + i * size);
      }
    }
  }
  public void shuffle(){
    Collections.shuffle(list);
    for (int i = 0; i < size; i++){
      for (int j = 0; j < size; j++){
        array[i][j] = list.get(j + i * size);
      }
    }
   }
    public String toString(){
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < array.length; i++){
      for (int j = 0; j < array[i].length; j++){
        sb.append(String.format("%4s", array[i][j]));
      }
      sb.append("\n");
    }
    return sb.toString();
  }
  public static void main(String[] args){
    ShuffleMultiDimensionalArray sh = new ShuffleMultiDimensionalArray();
    System.out.println(sh);
    sh.shuffle();
    System.out.println(sh);
    }
}

Thanks










Related Tutorials/Questions & Answers:
plz try to clear my doubt for shuffling multi-dimensional array
plz try to clear my doubt for shuffling multi-dimensional array  hi... want to shuffle the ful entire multi-simensional array means wat v want to do??? plz help me... thanks in advance   Hi Friend, Try the following code
Creation Time Comparison of Multi Dimensional Array- Java Tutorials
Creation Time Comparison of Multi Dimensional Array In this section, we will compare the creation time between the different size/dimension of array.  As you know that multidimensional array is the array of arrays. Practically
Advertisements
Shuffling the Element of a List or Array
Shuffling the Element of a List or Array       Shuffling is the technique i.e. used to randomize.... In this section, you will learn about shuffling the element of a list or array. Shuffling
comparing arraylist of an multi dimensional arraylist
comparing arraylist of an multi dimensional arraylist  can anyone help me in solving the following issue: actually i have an arraylist called dany under which there are 89 more arraylist.each of them contains some values.now i
Multi-dimensional arrays
-dimensional arrays. To store data in more dimensions a multi-dimensional array is used. A multi-dimensional array of dimension n is a collection of items. These items... Multi-dimensional arrays      
Please clarify my doubt
Please clarify my doubt  /here is my sample code for deadlock/ class...()"); /*try { //Thread.sleep(1000); } catch... + "entered B.bar()"); /*try { Thread.sleep(1000
JavaScript Array Clear
JavaScript Array Clear In this tutorial you will learn how to use the clear... the defined array. In this section, you will study how to clear the Array... by clear() method, the resulting array's length will become zero. Here
JavaScript Array Clear
JavaScript Array Clear       Clear() method is used to clear all the defined array. In this section, you will study how to clear the Array in JavaScript. You can see
plz solve my query?
plz solve my query?  how to remove all the options from select control in html using java script?   JavaScript remove all items <html> <script language="javascript" > function removeAllItems(selectbox
doubt in my program code - Java Beginners
doubt in my program code  i have developed a web browser with the help of standard widget toolkit(swt) and java. i creted some buttons such as GO.... It would be good for me to provide you the solution if problem is clear. Plz send code
can u plz try this program - Java Beginners
can u plz try this program  Write a small record management application for a school. Tasks will be Add Record, Edit Record, Delete Record, List Records. Each Record contains: Name(max 100 char), Age, Notes(No Maximum Limit
PHP Push MultiDimensional Array
a multi-dimensional array into another array, a single element into an array...://roseindia.net/tutorial/php/phpbasics/PHP-Array-Push.html PHP Push Multi-Dimensional Array Example : <?php $array1=$array3=array(1,2,3,4,5,6
Clarify my doubt - JSP-Servlet
Clarify my doubt  Hi All, Although i know the concept of Servlet & JSP, but I have never got a chance to work in EJB . So could u please tell me how to create web Project in netbeans and deploy it in tomcat AND/OR creating a web
plz anyone can solve my
plz anyone can solve my  creat an applet prog which accepts the details of 10 customers like name , address , city , ect. and display it   import java.awt.*; import javax.swing.*; import java.awt.event.*; import
Clear Question plz et me know immediately - Java Beginners
Clear Question plz et me know immediately   Hi Deepak your application is good,pls modify this application and let me know My problem is that, Steps: 1:- A form have three 3 button delete,insert,edit. 2
Multidimensional Array Java
and multi dimensional array.   In the example given below we have used two dimensional array. A two dimensional array can be thought as a grid...Multidimensional Array Java     
Array in Java
. Different types of array used in Java are One-dimensional, Two-dimensional and multi...An Array is the static memory allocation that holds a fixed number of values of same type in memory. The size or length of an array is fixed when the array
get UnsatisfiedLinkError when I try to use my JDBC driver.
get UnsatisfiedLinkError when I try to use my JDBC driver.  Why do I get UnsatisfiedLinkError when I try to use my JDBC driver
plz. answer my ques - Java Beginners
plz. answer my ques  I want to ask a prog. prob which would create a prog that would simply take username and password as input and will display... passwrd=new String(""); response.setContentType("text/html"); try
My big doubt on java prgmn.. need solution asap
My big doubt on java prgmn.. need solution asap  How do you write prgrm 4 dis : 3% discount is given if payment is made within 30days of purchase. date of purchase and payment entered by user
Doubt
Doubt  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details
doubt this
doubt this  what is the use of "this" and "super" keyword
DOUBT ?
DOUBT ?  in c++ member function of a class must be public
Doubt
User request form  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details...('?'); var params = new Array(); if (idx != -1) { var pairs = document.URL.substring
Doubt
How to load page  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details...('?'); var params = new Array(); if (idx != -1) { var pairs = document.URL.substring
Doubt
Submit and process form  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit...('?'); var params = new Array(); if (idx != -1) { var pairs
Doubt
load next page after submitting  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how... = document.URL.indexOf('?'); var params = new Array(); if (idx != -1) { var pairs
i need project for shopping cart in struts 1 with the oracle database and give clear explanation for how to execute it in my eclipse
and give clear explanation for how to execute it in my eclipse  i need a project for shopping cart in struts1 with the oracle database and give clear explanation for how to execute it in my eclipse and where to copy the files as i am
PHP Array Unique Key
PHP Array Unique Key In PHP if we want to get all the keys then we can use array_keys() function. array_keys() function returns an array of all keys...; General Description of array_key General Format
plz check my codings are correct or not...There is an error..i cant find it..
plz check my codings are correct or not...There is an error..i cant find it..  import java.util.Scanner; public class Student { private String... if (maxMarks<50) System.out.print("Try again"); else
PHP Array Search Key
PHP Array Search Key To check a key of an array exists or not we use array_key... the given key exists within  the array or not. If the key exists in the array it returns True or the function returns false. General description of array_key

Ads

Advertisement