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
-----

how do we know the “length” of an enum.

The values() method returns an array of the contents.
Use the .length property on that.

NormR1, thank you very much for your advise. Accordingly, I would like to replace the code on line 5 in the first poster by:

String pc [][]=new String[Suit.values().length][Rank.values().length];

It works.

One should declare a String paker[][]= new String[4][13]; to store all (52) the cards

IMO better to have a "Card" class with "rank" and "suit" as member variables. The toString() of this class would take care of concatenating the toString() method output of the respective "card" and "rank" enum members. Here is a simple example taken from the Java specification:

import java.util.*;
public class Card implements Comparable<Card>, java.io.Serializable {
    public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,JACK, 
QUEEN, KING, ACE }
    public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
    private final Rank rank;
    private final Suit suit;
    private Card(Rank rank, Suit suit) {
        if (rank == null || suit == null)
            throw new NullPointerException(rank + ", " + suit);
        this.rank = rank;
        this.suit = suit;
    }
    public Rank rank() { return rank; }
    public Suit suit() { return suit; }
    public String toString() { return rank + " of " + suit; }
    // Primary sort on suit, secondary sort on rank
    public int compareTo(Card c) {
        int suitCompare = suit.compareTo(c.suit);
        return (suitCompare != 0 ? suitCompare : rank.compareTo(c.rank));
    }
    private static final List<Card> prototypeDeck = new ArrayList<Card>(52);
    static {
        for (Suit suit : Suit.values())
            for (Rank rank : Rank.values())
                prototypeDeck.add(new Card(rank, suit));
    }
    // Returns a new deck
    public static List<Card> newDeck() {
        return new ArrayList<Card>(prototypeDeck);
    }
}

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

enum is a keyword, Enum is a class. enum is a syntactic sugar for easing the task of creating custom enum implementation. Enum class provides the base functionality required by all enumerations irrespective of their application specific use. enum declarations are automagically (by the compiler) converted to a final class which extends Enum. Since this is done automatically, you as an end user of enum need not bother with explicitly extending Enum class.

package test;

enum Digit {
  ZERO
};

// would be expanded to something like below (output from javap)

final class test.Digits extends java.lang.Enum {
    public static final test.Digit ZERO;

    static {};
    public static home.projects.misc.classloader.Digit[] values();
    public static home.projects.misc.classloader.Digit valueOf(java.lang.String);
}

In the above code snippet, the static part and the methods values and valueOf would be generated specific to Digits class. The static part would initialize the static final member ZERO and push it into the set of enum values for this enumeration. The values method would contain the logic to return the enumeration values contained in this enum and the valueOf method would take care of returning the enum object based on the enum value passed in as a String (in our case a string "ZERO").

Why any class defined by a programmer can not inherit Enum?

See above. Also, this is enforced at the compiler level and is probably a part of the Java language specification. The reason is that it is much easier to generate a user defined Enum class than rely on the programmer to do the same. Also, checking for each and every possibility and making sure that the compiler emits correct errors if the enum specification is violated by the programmer is much difficult hence the limitation.

Also, there are specific collection classes called EnumSet and EnumMap in case you decide to mess around quite a bit with enums.

SOS, I have learned a lot from your poster. The answers are highly appreciated.

SOS, I have learned a lot from your poster. The answers are highly appreciated.

You are welcome. Please make sure you mark the thread as solved if your queries have been answered.

Also, it's "post" and not "poster". "Poster" is someone who makes the "post". :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.