GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 493 of 502    Print  
Jumble Array Values
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.



  
Total Answers and Comments: 7 Last Update: August 13, 2009     Asked by: venkatalaks 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: c4crab
 
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;

public class Test {
    public static void main(String[] args) {
        HashMap hm = new HashMap();
        int arr[] = new int[20];
        for (int i = 0 ;i<20; i++)
        {
            arr[i]=i;
            hm.put(String.valueOf(new Random().nextInt()),String.valueOf(i));
        }
        Collection arr1 = hm.values();
        Iterator i = arr1.iterator();
        while (i.hasNext())
        {
            System.out.println((String)i.next()+" ");
        }
    }
}


Above answer was rated as good by the following members:
jebadevi, ahamza59, alok2009
September 27, 2008 13:29:57   #1  
vinshv Member Since: September 2008   Contribution: 1    

RE: Jumble Array Values

public class Main {
static String[] A new String[] {"1" "2" "3" "4"};

private static String toString(String[] s) {
String result "";
for (int k 0; k < s.length; k++) {
result + s[k];
}
return result;
}

private static void swap(int i int j) {
String tmp A[i];
A[i] A[j];
A[j] tmp;
}

public static void main(String[] args) {
int i j;

System.out.println(toString(A));

for (i 0; i < A.length; i++ ) {
for (j i+1; j < A.length; j++) {
swap(i j);
System.out.println(toString(A));
}
}
}
}


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
October 17, 2008 09:45:18   #2  
rjlast888 Member Since: October 2008   Contribution: 1    

RE: Jumble Array Values
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


public class TestProgram
{

/**
* @param args
*/
public static void main(String[] args)
{

int n 20;
List a new ArrayList(n);
List randArray new ArrayList(n);
Map usedMap new HashMap();

for(int idx 0; idx < n; idx++)
{
a.add(new String(Integer.toString(idx)));
}

Random generator new Random();
for(int idx 0; idx < a.size(); idx++)
{
int position getPosition(generator usedMap a.size());
randArray.add(a.get(position));
}

for(int idx 0; idx < randArray.size(); idx++)
{
System.out.println((String)randArray.get(idx));
}


}

private static int getPosition(Random generator Map usedMap int arraySize)
{
int pos -1;

while(pos -1 && !usedMap.containsKey(pos))
{
float f generator.nextFloat() * arraySize;
int irand (int)f;
if(irand arraySize)
{
irand--;
}
pos irand;
}

usedMap.put(pos null);

return pos;
}

}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
October 30, 2008 15:01:40   #3  
c4crab Member Since: October 2008   Contribution: 1    

RE: Jumble Array Values
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;

public class Test {
public static void main(String[] args) {
HashMap hm new HashMap();
int arr[] new int[20];
for (int i 0 ;i<20; i++)
{
arr[i] i;
hm.put(String.valueOf(new Random().nextInt()) String.valueOf(i));
}
Collection arr1 hm.values();
Iterator i arr1.iterator();
while (i.hasNext())
{
System.out.println((String)i.next()+" ");
}
}
}

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 2Overall Rating: -N/A-    
November 25, 2008 22:22:53   #4  
jweir Member Since: November 2008   Contribution: 1    

RE: Jumble Array Values
void jumble(Object[] A) {
Random rand new Random(System.currentTimeMillis());
Object temp;
for (int i 0; i < A.length; i++) {
int idx rand.nextInt(A.length);
temp A[idx];
A[idx] A[i];
A[i] temp;
}
}

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 02, 2009 12:16:13   #5  
jwfakejr Member Since: March 2009   Contribution: 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)+" ");

}
}
}

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 05, 2009 06:42:05   #6  
vijusatya Member Since: May 2009   Contribution: 3    

RE: Jumble Array Values

public void jumble()
{
String a new String{"A" "B" "C"};
ArrayList ar new ArrayList();
for (i 0;i< arr.length;i++)

{

String str new String(a[i]);
for(j 0;j< arr.length;j++)
{
if(!(a[i].equals(a[j])))
str str+a[j];

}
if(!ar.contains(str)
{
System.out.println(str);
ar.add(str);
}

}


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 13, 2009 04:16:46   #7  
Sarje Member Since: August 2009   Contribution: 62    

RE: Jumble Array Values
class JumbleArray
{
public static void main(String args[])
{
char charArr {"A" "B" "C"};
String str new String(charArr);
StringBuilder sb;
char ch;
for(int i 0;i<charArr.length;i++)
{
ch str.charAt(0);
str str.substring(1);
str str + ch;
System.out.println(str);
sb new StringBuilder(str);
sb sb.reverse();
System.out.println(sb);
}
}
}


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape