-4

I am trying to write a method which return the playing cards (Array<String>) in reverse order.

Each card is represented as a combination of : card values (A, 1-9, T, J,Q,K) and suits (h=hearts, d=diamonds, c=clubs, s=spades) also 10 is always represented by T not by 10 as two characters.

How to write a reverse method for Array<String> in Java?

public Array<String> reverseDeck(Array<String> deckOfCards) { 

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user261002
  • 2,182
  • 10
  • 46
  • 73
  • 4
    1) See [Starting Writing a Program](http://home.earthlink.net/~patricia_shanahan/beginner.html) for great tips. 2) [What have you tried?](http://www.whathaveyoutried.com/) I mean *besides* asking us. – Andrew Thompson May 09 '13 at 15:10
  • 1
    1) search google for "reverse array java" 2) click the link http://stackoverflow.com/questions/9995432/reverse-array-order 3) follow the instructions there. – John Dvorak May 09 '13 at 15:13
  • @AndrewThompson it is protected now. As in, not visible from outside. Why did you remove it? – John Dvorak May 09 '13 at 15:15
  • @Baadshah Oh write. You're welcome. :) – Andrew Thompson May 09 '13 at 15:17
  • @JanDvorak As in, not visible from outside. Why did you remove it? what does it mean ? – Suresh Atta May 09 '13 at 15:19
  • @Baadshah "not visible from the outside" - I was referring to the meaning of `protected` in Java and the lack of the tag in the title. "why did you remove it?" - I was referring to the removal of the tag from the title. I see no reason to remove "in Java" (as opposed to "[java] how do I..." or "java - how do I...") – John Dvorak May 09 '13 at 15:22

1 Answers1

1

Try this:

public ArrayList<String> reverseDeck(ArrayList<String> deckOfCards)
{
   ArrayList<String> reversedDeck = new ArrayList<String>(deckOfCards.size());

   for(int i=deckOfCards.size()-1;i>=0;i--)
       reversedDeck.add(deckOfCards.get(i));

   return reversedDeck;
}
Jops
  • 22,535
  • 13
  • 46
  • 63