Categories: java, arrays, sorting

How do I transfer the existing String array to 2D array

1 answer

I am trying to get my private void transfer method and pass it into a 2D array transfer2D method, after which I am able to print it out using the print2D_1 method. I am also trying to sort the array from the highest suit to the lowest suit followed by the highest rank to the lowest rank e.g. S12, S9, H13, D09, C10. Please advice.

import java.util.ArrayList; import java.util.EnumSet; import java.util.Random; import java.util.concurrent.ThreadLocalRandom;  enum SuitEnum {     Spade ('S'),     Heart ('H'),     Diamond ('D'),     Club ('C');  public char suit;  SuitEnum(char suit) {     this.suit = suit; } // Accessor getter public char getSuit () {     return suit; } }  enum RankEnum {     Two ('2'),     Three ('3'),     Four ('4'),     Five ('5'),     Six ('6'),     Seven ('7'),     Eight ('8'),     Nine ('9'),     Ten ('T'),     Jack ('J'),     Queen ('Q'),     King ('K'),      Ace ('A');      public char rank;      RankEnum(char rank)     {         this.rank = rank;     } // Acessor getter     public char getRank ()     {         return rank;     } }  class PlayingCard {     private SuitEnum suit;     private RankEnum rank;     private PlayingCard pc;      //constructor     public PlayingCard(SuitEnum suit, RankEnum rank)     {     this.suit = suit;     this.rank = rank; }  //copy constructor public PlayingCard(PlayingCard pc) {     this.pc = pc; }  //accessor get method public SuitEnum getSuit() {     return suit; }  public RankEnum getRank() {     return rank; }  //setter public void setCard(SuitEnum suit, RankEnum rank) {     this.suit = suit;     this.rank = rank; }  @Override public String toString() {     return String.format("%3s%s",suit.getSuit(),rank.getRank()); } }  class ChuaWeiheng_A1  { private final int MAXC = 13;  private final int MAXD = 52;  private void deckOfCards(ArrayList<PlayingCard> values) {     for (SuitEnum suit : SuitEnum.values())     {         for (RankEnum rank: RankEnum.values())             values.add(new PlayingCard (suit, rank));     } }  private void printDeck(ArrayList<PlayingCard>values) {     int count = 0;      System.out.println("Printing from ArrayList");     System.out.println();     {         for (PlayingCard s : values)         {             count++;             System.out.print(s);              if (count == MAXC)             {             System.out.println();             count = 0;               }                      }     }     System.out.println("-------------------------");  }  private void listToArray(ArrayList<PlayingCard> values, PlayingCard[] valuesArray) {     int i = 0;     for (PlayingCard s: values)     {         valuesArray [i] = s;         ++i;     } }  private void printDeck(PlayingCard[] valuesArray) {     int count = 0;     System.out.println("Printing from Array");     System.out.println();       for (PlayingCard s : valuesArray)     {         count++;         System.out.print(s);          if (count == MAXC)         {         System.out.println();         count = 0;         }                  }     System.out.println("-------------------------");  }  private void transfer(PlayingCard[] cardArray, String[] strArray) {     String[] arrayNo = new String[]{"02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14"};     String[] arrayLetter = new String[]{"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};     int i = 0;     for (PlayingCard s : cardArray)     {         String currentCard = s.getRank().toString();          for (int j = 0; j < 13; j++)         {             if (currentCard.equals(arrayLetter[j]))             {                 strArray[i] = s.getSuit().toString().charAt(0) + arrayNo[j];             }         }                i++;     } }  private void printStringArray(String[] strArray) {     int count = 0;     System.out.println("Printing from string array");     System.out.println();     for (int i = 0; i < MAXD; i++)     {         count++;         System.out.print("  " + strArray[i]);          if (count == MAXC)         {             System.out.println();             count = 0;         }                 }          System.out.println("-------------------------"); }   // shuffle  private void shuffle(PlayingCard[] valuesArray) {     Random random = ThreadLocalRandom.current();     int count = 0;     for (int k = valuesArray.length -1; k> 0; k --)     {         int i = random.nextInt(k+1);         PlayingCard j = valuesArray[i];         valuesArray[i] = valuesArray[k];         valuesArray[k] = j;     }     System.out.println("Shuffle the cards - Array Version");     System.out.println("Printing from array");     System.out.println();     {         for (PlayingCard s : valuesArray)         {             count++;             System.out.print(s);              if (count == MAXC)             {             System.out.println();             count = 0;             }                      }                System.out.println("-------------------------");             } }  private void transfer2D(String[][] twoD, String[] strArray) {     int rows = 4;     int columns = 13; twoD = new String[rows][columns];  for(int i = 0; i < twoD.rows; i++)     {            for(int j = 0; j < twoD.columns; j++)         {             twoD[i][j] = strArray[(i*twoD.columns)+j];         }     } }  private void print2D_1(String[][] twoD) {     int count = 0;     System.out.println("Printing from string array");     System.out.println();      for(int[] a : twoD)     {         for(int i : a)         {         }      } }  public static void main(String[] args)  {     ArrayList<PlayingCard>X = new ArrayList<PlayingCard> ();        ChuaWeiheng_A1 T1 = new ChuaWeiheng_A1();     PlayingCard [] T2 = new PlayingCard[T1.MAXD];     String[] strArr = new String[52];      T1.deckOfCards(X);     T1.printDeck(X);     T1.listToArray(X,T2);     T1.printDeck(T2);     T1.shuffle(T2);     T1.transfer(T2, strArr);     T1.printStringArray(strArr); }    } 

All answers to this question, which has the identifier 61238660

The best answer:

Your transfer2D() method should look like this:

private void transfer2D(String[] strArray, String[][] twoD) {   int i = 0;   for( int suit = 0; suit < 4; suit++ )     for( int rank = 0; rank < 13; rank++ )       twoD[suit][rank] = strArray[i++]; } 

as a convention: outgoing values should appear at the end of the parameter list

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data