Results 1 to 2 of 2
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default In a 2D array, how to initialize each cell to a enum type object

    Hi. If I was making a board game like chess. And say I have some a public enum ChessPiece . There are pawns, knights, kings, queesn, bishop, rooks, and empty. How do I make a 2D array that stores, for example, empty.

    Java Code:
    private Object board;
    
    
    //constructor making a new board with nothing in it
    chess(){
    
    board = new Object[8][8];
    
      for ( int i = 0; i < 8; i++){
         for  (int j = 0; j < 8; j++){
    
            board[i][j] =  ChessPiece.empty         // Thats what I want to do, I know this doesn't work
    Thank you for help.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: In a 2D array, how to initialize each cell to a enum type object

    Can't you include a VACANT type field in your enum declaration? And why is your board declared type Object and not your enum type?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Enum Type
    By kashyapkeshav in forum New To Java
    Replies: 3
    Last Post: 08-19-2012, 05:08 PM
  2. java enum : type safety
    By rsai in forum New To Java
    Replies: 2
    Last Post: 07-05-2012, 04:45 AM
  3. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  4. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 07:02 PM
  5. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 07:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •