1

Sorry, I'm totally new to Java maybe my question a bit stupid, but please help me. so, i have 2 arrays here:

char[] alphabet = {'A','B','C','D','E','F','G','H','I','J'
                   ,'K','L','M','N','O','P','Q','R','S','T'
                   ,'U','V','W','X','Y','Z'};     
char[] numberArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

and then I already merged both arrays in one new array then randomize it, and for example the output would be like:

4 Z B 8 R W P F T 1 D H S L Q 2 N J 5 6 V 3 A C 0 G K U E X 7 O Y M 9 I 

The code I've written to merge two arrays is the following:

public static char[]merge(char[]alphabet, char[]numberArray){
    char[] both = new char[alphabet.length+numberArray.length];
    int i;
    for(i=0; i<alphabet.length; i++)
        both[i] = alphabet[i];
    for(int j=0; j<numberArray.length; j++)
        both[i++]=numberArray[j]; return both;
}

then from this output I wanna make all these elements into several 2D arrays with randomized elements. like this

G 4
7 Y

Or

H T
U 8

and so on.. how can i make the output like that? how to make it into 2x2 arrays?

oh, to make it clear, i wanna make it into more than 1 of 2x2 arrays, and generate it automatically, maybe I need loops to generate them.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    Good to see you learning these coding techniques. Please include your merging code to see how far you've gone. – itsols Dec 21 '15 at 15:31
  • Do the characters in your 2 x 2 arrays have to be unique? – Gilbert Le Blanc Dec 21 '15 at 15:40
  • @GilbertLeBlanc I'm going to assume yes. He wants to (I think) randomize characters from the alphabet and number array, which contain only unique characters to begin with. The 2x2 would then be comprised of random characters from each array, which means they will be unique anyway. – 17slim Dec 21 '15 at 15:43
  • each element has to be unique, but only in their assigned array. another arrays can be have the same elements with another one. – fuad baskara Dec 21 '15 at 15:45
  • i'm still confused how to insert my code in a comment haha, it's not like how i was make the question – fuad baskara Dec 21 '15 at 15:47

3 Answers3

2

Read this answer to understand how to create 2D arrays in Java.

If you understand how to create it, you will understand how to populate it. Then you can select elements at random with the Random class, for example:

// generates an integer between 0 (inclusive) and arrayLength (exclusive)
new Random().nextInt(arrayLength); 
Community
  • 1
  • 1
cahen
  • 15,807
  • 13
  • 47
  • 78
0

You can shuffle your array with Fisher-Yates algorithm, as stated here.

  // Implementing Fisher–Yates shuffle
  static void shuffleArray(char[] ar)
  {
    // If running on Java 6 or older, use `new Random()` on RHS here
    Random rnd = ThreadLocalRandom.current();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }

After you randomized the merged array, this should do the trick:

char[][][] your_array = new char[9][2][2]
for (int i = 0; i < 9; i++) {
    your_array[i][0][0] = randomized_array[4*i]
    your_array[i][0][1] = randomized_array[4*i + 1]
    your_array[i][1][0] = randomized_array[4*i + 2]
    your_array[i][1][1] = randomized_array[4*i + 3]
}

You can access each of the 2x2 arrays like this: your_array[0] returns the first one, your_array[0] the second, etc.

Community
  • 1
  • 1
Gabriel Ilharco
  • 1,649
  • 1
  • 21
  • 34
0

Another answer

first, you can use collections, without loop:

// just change char to Character 

List<Character> alpha =Arrays.asList(alphabet);
List<Character> numb =Arrays.asList(numberArray);
List<Character> all=new ArrayList<Character>();
all.addAll(alpha);
all.addAll(numb);
Character [] both2=all.toArray(new Character[all.size()]);

then for 4 values, take 4 indexes at random between all (some loops):

// SELECTING
int how_many=10;
int total_size=all.size();

char[][][] ard2=new char[how_many][2][2];

for (int k=0;k<how_many;k++)
    {
    char[][] current_ard2=ard2[k];

    // Take 4 chars differents, by taking 4 different indexes
    int[] idx=new int[4];
        for (int j=0;j<4;j++)
            {
            boolean not_done=true; 

            while (not_done)
                {
                boolean done=true; // by default
                int candidate=(int) (Math.random()*total_size);

                // check precedent values
                for (int h=0;h<j;h++)
                    if (idx[h]==candidate)
                    {// lost !
                    done=false;
                    break;
                    }
                if (!done) continue; // candidate already exists

                // bingo
                idx[j]=candidate;
                not_done=false;
                }           
            } // for (int j=0;j<4;j++)

    current_ard2[0][0]=both2[idx[0]];
    current_ard2[0][1]=both2[idx[1]];
    current_ard2[1][0]=both2[idx[2]];
    current_ard2[1][1]=both2[idx[3]];

    System.out.println(current_ard2[0][0]+" "+current_ard2[0][1]+"\n"+current_ard2[1][0]+" "+current_ard2[1][1]+"\n");
    } // for (int k=0;k<how_many;k++)

Hope it helps