I am interested in Current threads, such as :
rare program :s
http://www.daniweb.com/forums/thread300723.html
how to create 2D array of enumeration values
http://www.daniweb.com/forums/thread300738.html
which have a common goal: to store the names of Enums objects in a 2D array of String.
For example, we have a pack of porker cards defined by the following two enums:
enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
(1) One should declare a String paker[][]= new String[4][13]; to store all (52) the cards.
(2) If one is asked to store two enums in a 2D array of String respectively one may create a 2D array to have their names:
String set[][]= new String[2][];
set[0]= new String[2];
set[1]= new String[13]
I have written a program where the 52 (4×13) cards are presented. However, I have following questions. Please help me to understand Enum in Java correctly.
1. Does the class Enum has a method by which one may know how many objects in this Enum class. That is, how do we know the “legnth” of an enum.
2. In API one may see Enum class while in coding one sees enum. Hence I would ask what is the differences between Enum and enum?
3. Why any class defined by a programmer can not inherit Enum?
I appolozige in advance for the incorrect/improper terms I used in current poster.
public class PokerCard {
enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
public static void main(String args[]){
String pc [][]=new String[4][13];
for ( Suit suit : Suit.values() )
for (Rank rank: Rank.values())
pc[suit.ordinal()][rank.ordinal()]= rank.name() + " of " + suit.name();
for (int i=0; i< pc.length; i++){
for(int j=0; j<pc[i].length;j++)
System.out.print(pc[i][j] + " ");
System.out.println();
System.out.println("-----");
}
}
}
Output:
DEUCE of CLUBS THREE of CLUBS FOUR of CLUBS FIVE of CLUBS SIX of CLUBS SEVEN of
CLUBS EIGHT of CLUBS NINE of CLUBS TEN of CLUBS JACK of CLUBS QUEEN of CLUBS KIN
G of CLUBS ACE of CLUBS
-----
DEUCE of DIAMONDS THREE of DIAMONDS FOUR of DIAMONDS FIVE of DIAMONDS SIX of DIA
MONDS SEVEN of DIAMONDS EIGHT of DIAMONDS NINE of DIAMONDS TEN of DIAMONDS JACK
of DIAMONDS QUEEN of DIAMONDS KING of DIAMONDS ACE of DIAMONDS
-----
DEUCE of HEARTS THREE of HEARTS FOUR of HEARTS FIVE of HEARTS SIX of HEARTS SEVE
N of HEARTS EIGHT of HEARTS NINE of HEARTS TEN of HEARTS JACK of HEARTS QUEEN of
HEARTS KING of HEARTS ACE of HEARTS
-----
DEUCE of SPADES THREE of SPADES FOUR of SPADES FIVE of SPADES SIX of SPADES SEVE
N of SPADES EIGHT of SPADES NINE of SPADES TEN of SPADES JACK of SPADES QUEEN of
SPADES KING of SPADES ACE of SPADES
-----