0

Hi Im a newbie at java programming and i have a problem with getting an element from my 2d string array plss help me

Here is my array

String[][] cardDeck = { 
            { "AceH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JackH", "QueenH", "KingH" }, 
            { "AceD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JackD", "QueenD", "KingD" },
            { "AceS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JackS", "QueenS", "KingS" },
            { "AceC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JackC", "QueenC", "KingC" }};
1
  • You're only picking a total of one card? Else what are you going to do if you pick the same card twice? Commented Sep 7, 2021 at 9:34

4 Answers 4

1
String[][] cardDeck = {
        { "AceH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JackH", "QueenH", "KingH" },
        { "AceD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JackD", "QueenD", "KingD" },
        { "AceS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JackS", "QueenS", "KingS" },
        { "AceC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JackC", "QueenC", "KingC" }};
    Random random = new Random();
    int dim1Pos = random.nextInt(cardDeck.length); // pick random position for 1st dimension (row)
    int dim2Pos = random.nextInt(cardDeck[dim1Pos].length); // pick random position for 2nd dimension (column) for previously selected row
    String randomString = cardDeck[dim1Pos][dim2Pos];
    System.out.println(randomString);
Sign up to request clarification or add additional context in comments.

Comments

1

How about this:

String[][] cardDeck = { 
    { "AceH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JackH", "QueenH", "KingH" }, 
    { "AceD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JackD", "QueenD", "KingD" },
    { "AceS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JackS", "QueenS", "KingS" },
    { "AceC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JackC", "QueenC", "KingC" }};
int random_y = (int)(Math.random() * cardDeck.length);
int random_x = (int)(Math.random() * cardDeck[random_y].length);
System.out.println(cardDeck[random_y][random_x]);

Math.random() returns a random DECIMAL between 0 and 1, so if we multiply it by some number a, we get a random decimal between 0 and a (non-inclusive). Converting it to int, we then get a random integer between 0 and a. And that's exactly what we need to get a random index!

But in this case, we'll need 2 random integers, since this is a 2D array. You can call them random_x and random_y, since they'll correspond to the random column and random row respectively. So we can access the random element using cardDeck[random_y][random_x].


If you're still confused as to why we multiply it by the length of the array, consider this: to get a random row, we need to access cardDeck[i], where i is an integer between 0 and cardDeck.length. In this particular row of the matrix, we want a random element. So we access it using cardDeck[i][j], where j is an integer between 0 and cardDeck[i].length.

3 Comments

how about if I only need 1 random element?
This is only for getting 1 random element. It's just that you'll need 2 random indexes to get that random element, since this is a 2D matrix. If this were a 3D matrix, you'd need 3 random indexes, and so on.
@user16320675 That's a good point, I'll edit that into my answer. Thanks!
1

Instead of using the Math.random() method, you could just use the Random class.

String[][] cardDeck = { 
    { "AceH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JackH", "QueenH", "KingH" }, 
    { "AceD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JackD", "QueenD", "KingD" },
    { "AceS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JackS", "QueenS", "KingS" },
    { "AceC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JackC", "QueenC", "KingC" }};
Random random = new Random();
int randomX = random.nextInt(cardDeck.length);
int randomY = random.nextInt(cardDeck[0].length);
System.out.println(cardDeck[randomX][randomY]);

Random is a class which can generate random numbers, in fact, the Math.random() method uses the Random class itself. random.nextInt(cardDeck.length); generates a random integer between 0(inclusive) and the number of rows(exclusive), which is a valid index. Similiarly, random.nextInt(cardDeck[0].length); will return a random integer between 0(inclusive) and the number of columns(exclusive), which too is the valid index.

Comments

1

You could use Streams to collect all elements to a list, and then shuffle the list:

List<String> list = Arrays.stream(cardDeck).      //stream to String[]
                    flatMap(x->Arrays.stream(x)). //stream to String
                    collect(Collectors.toList()); //collect to a List 

Collections.shuffle(list);    //shuffle collection
String randomElement = list.get(0); //get an arbitrary element 

You could also use Streams.skip(n) :

Optional<String> randomElement =  Arrays.stream(cardDeck).  //stream to String[]
                                 flatMap(x->Arrays.stream(x)). //stream to String
                                 skip(new Random().nextInt(cardDeck.length*cardDeck[0].length ))//remove random number of elements 
                                .findFirst();

4 Comments

You can also reduce your map taking random value form a pair.
@AlexeyR. please post an answer and show how
Cannot do that from my cell phone.. Basically there would be flatMap(x->Arrays.stream(x)).reduce((s, s2) -> random.nextBoolean() ? s : s2).get() where you initialize random before..
I see what you mean. It needs to be improved because as is 50%of the times it will return last element of the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.