how to create 2D array of enumeration values

foe example i have

enum Days {Day1,Day2,Day3}
enum Subjects {Subject1,Subject2,Subject3}

how can i create its two dimension array two show these values in rows and coloumns

Iterate over the enum, for each value, invoke the name() method which will return the name as String. You can then store the name in a 2d array of String.

how to create 2D array of enumeration values

foe example i have

enum Days {Day1,Day2,Day3}
enum Subjects {Subject1,Subject2,Subject3}

how can i create its two dimension array two show these values in rows and coloumns

public class EnumProblem {

  enum Days {Monday, Tuesday, Wednesday, Thursday, Friday}
  enum Periods {Period1, Period2, Period3, Period4, Period5}

  // Define 2 dim array using the above enums  
  Object[][] twoDim = new Object[][] {{Days.Monday, Periods.Period1}, {Days.Tuesday, Periods.Period2}};
}
public class EnumProblem {

  enum Days {Monday, Tuesday, Wednesday, Thursday, Friday}
  enum Periods {Period1, Period2, Period3, Period4, Period5}

  // Define 2 dim array using the above enums  
  Object[][] twoDim = new Object[][] {{Days.Monday, Periods.Period1}, {Days.Tuesday, Periods.Period2}};
}

i want to do this using String , not Object ...when i am writing String instead of Object it is giving an error.

Here is the code.

class EnumProg {
  enum Days {Mon, Tues, Wed, Thurs, Fri}
  enum Periods {Period1, Period2, Period3, Period4, Period5}
  public static void main (String [] args)
  {
  String[][] twoDim; 
  twoDim = new String[5][5] ;
  {
  for ( Days day : Days.values() )
      {
        for (Periods prds : Periods.values() )
        System.out.println (twoDim[day.name()][prds.name()]);
      }
  }
 }
}

Edited 6 Years Ago by Xufyan: n/a

it is giving an error.

Please copy and paste here the full text of the error message.

The indexes to arrays should be integers not Strings.

Edited 6 Years Ago by NormR1: n/a

here is the error, please ghelp me creating 2D array of enum values in rows and coloumns

EnumProg.java:12: incompatible types
found   : java.lang.String
required: int
        System.out.println (twoDim[day.name()][prds.name()]);
                                           ^
EnumProg.java:12: incompatible types
found   : java.lang.String
required: int
        System.out.println (twoDim[day.name()][prds.name()]);

The compiler is telling you that:
The indexes to arrays should be integers not Strings.

Use the ordinal() method to get an integer

how to use ordinal() method can you pelase help ?
i'm trying for two days to create 2D array from enum but didn't get it right :(
its really annoying....i tried thousands of way so far

class EnumProg {
  enum Days {Mon, Tues, Wed, Thurs, Fri}
  enum Periods {Period1, Period2, Period3, Period4, Period5}
  public static void main (String [] args)
  {
  String[][] twoDim; 
  twoDim = new String[5][5] ;
  {
  for ( Days day : Days.ordinal() )
      {
        for (Periods prds : Periods.ordinal() )
        System.out.println (twoDim[day.name()][prds.name()]);
      }
  }
 }
}

Edited 6 Years Ago by Xufyan: n/a

Alright i've create a Single Dimensional Array which is working fine....
now how can i create a 2D array.
here is the code.

class EnumProg {
  enum Days {Mon, Tues, Wed, Thurs, Fri}
  enum Periods {Period1, Period2, Period3, Period4, Period5}
  public static void main (String [] args)
  {
    Days day[] = Days.values();
    for(Days a : day)
      System.out.print(a + "\t ");
 }
}

how can i create a 2D array

What type of data do you want to be in the array?

<type>[][] nameOf array = new <type>[dim1][dim2];

Replace <type> with the data type or class.

i want to store the enum values in String array and access the values through array. here is the final program

class EnumProg {
  enum Days {Mon, Tues, Wed, Thurs, Fri}
  enum Periods {Period1, Period2, Period3, Period4, Period5}
  public static void main (String [] args)
  {
    for(Days day : Days.values()){
      System.out.print(day + "" + "\t\t");
    }
    System.out.println ("");
    for ( Periods period : Periods.values() ){
        for(int i=0; i<5; i++){
    System.out.print (period + "\t\t");
        }
    System.out.println ("");
  }
 }
}

store the enum values in String array

Isn't there a problem with mixed types here?
enums are not Strings.
You could use the .name() method to get Strings for each of the enum constants and store those strings in the array.

This works:

String[] enumNames = new String[] {Days.Monday.name(), Days.Tuesday.name()};
     System.out.println(Arrays.toString(enumNames));  // show array's contents: [Monday, Tuesday]

Edited 6 Years Ago by NormR1: n/a

I wonder if the following way is a candidate answer for Xufyan's question?
Should we make Days type 1D array and Periods type 1D array to play a go-between role? Then use the method name() to store the names in two final String arrays?

public class Enumeration0 {
  enum Days {Monday, Tuesday, Wednesday, Thursday, Friday}
  enum Periods {Period1, Period2, Period3, Period4, Period5}
  
    public static void main(String[] args){
    String sd[]=new String[5]; // allocate String array sd
    String sp[]=new String[5]; // allocate String array sp
    Days d[] = Days.values();  // create Days array d with values of Days
    Periods p[]=Periods.values(); // create Periods array p with values of Periods
    
    for(int i=0; i<5; i++){  // store the enum values in String arrays: sd and sp 
	sd[i]=d[i].name();
	sp[i]=p[i].name();
		}
    for (String  s : sd)   // print String array sd
	System.out.print(s + " ");
	System.out.println();
    for (String  s : sp)  // print String array sp
	System.out.print(s + " ");
	System.out.println();
	}
}

Edited 6 Years Ago by tong1: n/a

To store two Enum type objects:
enum Days {Mon, Tues, Wed, Thurs, Fri}
enum Periods {Period1, Period2, Period3, Period4, Period5}
in a 2D String array, one should start with the line of code :

String twoDim[][]= new String[2][5]; // Xufyan incorrectly wrote new String[5][5]

Then we may store the values in the String 2D array twoDim with the following code without "go-between". I also noticed that as NormR1 indicated, the method name() should be used since enums are not Strings.

twoDim = new String[2][5] ;
   int i=0;
  for ( Days day : Days.values() )
   twoDim[0][i++]=day.name();
      i=0;
  for (Periods prds : Periods.values()) 
   twoDim[1][i++]=prds.name();

Edited 6 Years Ago by tong1: n/a

One may also use the ordinal() method to provide index for the twoDim so that the int i could be saved.

String twoDim[][] = new String[2][5] ;
   for ( Days day : Days.values())
      twoDim[0][day.ordinal()]=day.name();

   for (Periods prds : Periods.values()) 
      twoDim[1][prds.ordinal()]=prds.name();

Edited 6 Years Ago by tong1: n/a

This article has been dead for over six months. Start a new discussion instead.