Appending two strings to one index in an Array in Java -


i've fixed how want now. if else has same issue think easiest , efficient way of trying set deck of cards. can pick out individual cards using random variable in deck[random][0] , deck[random][1]. help, here code:

public class newdeck {      public static void main(string[] args) {          string[] suits = new string[] { "clubs", "diamonds", "spades", "hearts" };          string[] faces = new string[] { "ace", "king", "queen", "jack" };          string[][] deck = new string[suits.length * (faces.length + 9)][2];          int = 0;          (string y : suits) {              (string x : faces) {                  deck[a][0] = x;                 deck[a][1] = y;                 a++;              }          }          (string y : suits) {              (int p = 2; p < 11; p++) {                  deck[a][1] = y;                 string pp = integer.tostring(p);                 deck[a][0] = pp;                 a++;              }          }          (int p = 0; p < deck.length; p++) {              system.out.print(deck[p][0] + " of ");             system.out.println(deck[p][1]);          }      }  } 

you should add parameter types strings , create pair class. note need java compiler of version 1.5 or higher generics.

class pair {       private final string face;        private final string suit;         pair(string suit, string face) {          this.face = face;           this.suit = suit;                   }      @override      public string tostring() {         return "(" + suit + ", " + face + ")";      } } 

then can use pair class follows, using appropriate list methods get , size:

list<pair> deck = new arraylist<pair>();  list<string> suits = new arraylist<string>(); suits.add("hearts"); suits.add("diamonds"); suits.add("clubs"); suits.add("spades");  list<string> faces = new arraylist<string>(); faces.add("ace"); faces.add("king"); faces.add("queen"); faces.add("jack");  for(int suit = 0; suit < suits.size(); suit++){      for(int face = 0; face < faces.size(); face++){          deck.add(new pair(suits.get(suit), faces.get(face)));                   }  } 

if override tostring method of pair can system.out.println(deck) desired string representation of arraylist.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -