GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java

 Print  |  
Question:  Jumble Array Values

Answer: An array A is of size N. Write a java method that jumbles all values in the array. All possible permutations must be possible and chances for each possible permutation must be equal. For example, size of array A is 3. A[0]=x, A[1]=y, A[2] =z.
Possible permutations are 3! After jumbling elements, there would be equal chances for each of xyz,yxz,... to be in the array elements.


June 06, 2009 12:16:13 #5
 jwfakejr   Member Since: March 2009    Total Comments: 1 

RE: Jumble Array Values
 
package javatest;

import java.util.ArrayList;
import java.util.Collections;

public class Main {
   
public static void main(String[] args) {

ArrayList<Integer> mylist = new ArrayList<Integer>();
for(int j = 0; j<20; j++)
{
  mylist.add(j);
 

}
Collections.shuffle(mylist);
System.out.println("n--------------");
for(int j = 0; j<20; j++)
{
 System.out.print(mylist.get(j)+" ");

}
// print it out again just to show it shuffles up the arrays contents
System.out.println("n--------------");
Collections.shuffle(mylist);
for(int j = 0; j<20; j++)
{
 System.out.print(mylist.get(j)+" ");

}
}
}
     

 

Back To Question