Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:42:13+00:00 2026-06-15T14:42:13+00:00

I have an question about how to shuffle an array. The background is: Write

  • 0

I have an question about how to shuffle an array. The background is:
Write a memory matching game, which using 16 cards that laid out in a 4*4 square and are labeled with pairs of number from 1 to 8.

Currently, what I need to do is to initialize these cards and shuffle these cards.

And now what I can think of is that creating a Card class including variable Rank from 1 to 8, secondly, naming a new class (matchingGame)(whatever you like) and writing a new static method: shuffle().

But I am stuck at this step.

My first question is how to initialize these 16 cards (8 pairs)?
(I think my code is not the efficient way).

My second question is how to shuffle cards after initialization?

My code is:

public class Card 
{
private int rank;

public Card(int iniRank)
{
    switch (iniRank)
    {
    case 1:
        rank =1;
        break;
    case 2:
        rank =2;
        break;
    case 3:
        rank =3;
        break;
    case 4:
        rank =4;
        break;
    case 5:
        rank =5;
        break;
    case 6:
        rank =6;
        break;
    case 7:
        rank =7;
        break;
    case 8:
        rank =8;
        break;
    }
}

public int getRank()
{
    return rank;
}


}


public static void initialize(Card[] cards)
{
    for (int i=0;i<2;i++)
    {
        cards[i] = new Card(1);
    }
    for (int i=2;i<4;i++)
    {
        cards[i] = new Card(2);
    }
....
}    

Thanks everyone for my previous question!
Just one more question in the same background, As your good advice, you know how to shuffle a 1D array?

But how to shuffle a 2D array? Obviously, I cannot use

List<Card> newcards = Arrays.asList(cards) 

for converting to List now.

Thanks for ur help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:42:15+00:00Added an answer on June 15, 2026 at 2:42 pm

    Your method for initializing the array is essentially correct: loop through your array and add a new instance at each point.

    Card[] cards = ...;
    for (int i = 0; i < cards.length; i++)
        cards[i] = ...  // new Card instance
    

    Now for shuffling – don’t reinvent the wheel: Use Collections.shuffle:

    Card[] cards = ...;
    List<Card> l = Arrays.asList(cards);
    Collections.shuffle(l);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question about XSLT1.0. The task is to write out in HTML all
I have question about database optimizing, indexing. I have table that called projects and
I have question about my code which does the Hoare Partition Method. Here is
I have question about benefit of using areas and necessity of them. I see
I have a question about equivalent search result, that can occur on different fields.
I have question about pointers in C. Each pointer has 4 bytes in memory
I have question about how to correctly write my application. I would like to
Hello I have question about android spinner I have spinner which is populated by
Hello I have question about android spinner I have spinner which is populated by
I have question about the need of using @configurable . I have gone through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.