0

Can I add multiple items, of the same type, in an index of an array in java? I am trying to make a backgammon program in java and I cannot figure out how to create the board. I need to store the 'checkers', 5 checkers in one position. Can I store 5 items in a single index. eg. array[1] = {a,b,c,d,e}. If i cant do that with an array, are there any other ways to do that.

This is what I have: (I do not have colours for now. ony variable homeWhite for whites and homeBlack for blacks)

  //other methods omitted

 /**
 *  a method to set the default board design and layout of the game.
 */
private void newBoard()
{

    homeWhite = 0;
    homeBlack = 0;
    barWhite = 0;
    barBlack = 0;
    private int [] stoneCounts = new int[25];

    stoneCounts[0]  = 5*homeWhite;
    stoneCounts[11] = 2*homeWhite;
    stoneCounts[16] = 3*homeWhite;
    stoneCounts[18] = 5*homeWhite;
    stoneCounts[23] = 5*homeBlack;
    stoneCounts[12] = 2*homeBlack;
    stoneCounts[7]  = 5*homeBlack;
    stoneCounts[5]  = 3*homeBlack;
    stoneCounts[24] = barWhite = barBlack;

    System.out.println(stoneCounts[0]+"===="+ stoneCounts[1]+"===="+stoneCounts[2]+"===="+stoneCounts[3]+"===="+ stoneCounts[4]+"===="+stoneCounts[5]+"===="+ 
    stoneCounts[6]+"===="+stoneCounts[7]+"===="+stoneCounts[8]+"===="+stoneCounts[9]+"===="+ stoneCounts[10]+"===="+stoneCounts[11]);
    System.out.println("");
    System.out.println("");
    System.out.println("Bar: "  + stoneCounts[24]);
    System.out.println("");
    System.out.println("");
    System.out.println(stoneCounts[12]+"===="+ stoneCounts[13]+"===="+stoneCounts[14]+"===="+ stoneCounts[15]+"===="+stoneCounts[16]+"===="+ stoneCounts[17]+"===="
    +stoneCounts[18]+"===="+stoneCounts[19]+"===="+stoneCounts[20]+"===="+ stoneCounts[21]+"===="+stoneCounts[22]+"===="+stoneCounts[23]);
}
2
  • You need an array of array.
    – Rohit Jain
    Nov 29, 2012 at 20:44
  • or array of int, where int is the number of pieces at that index. However, this does not say anything about the color of the piece at that index.
    – Colin D
    Nov 29, 2012 at 20:46

3 Answers 3

0

Since you have a set number of spaces and a variable number of pieces on each space, I would recommend an array of ArrayLists:

ArrayList<Piece>[] spaces = new ArrayList<Piece>[numSpaces];

for(int i=0; i<numSpaces; i++){
   spaces[i] = new ArrayList<Piece>();
}

This assumes you have a Piece class, which could store the color of the piece or any additional info. Then whenever you want to add a piece, you can just do spaces[i].add(piece); or whatever. Hope that helps!

0

Ok, there are two ways you might consider. Firstly, you can create a two dimensional array. I am not sure from your question exactly what type of information you want in your array, but for example if we assume you wish to create a two dimensional array of ints, you can then adapt the code to how you like, all you need to do is change the type (int) to something else for example String, or char. This is just general information for you to help you make a 2D array and understand how it works.

int[] intArray = new int[5];

This will create you an array of ints which will accept up to 5 different values. You will have intArray[0], intArray[1], intArray[2], intArray[3] and intArray[4]. You may wish to visualise this as 5 spaces like this:

[]  []  []  []  []

However, what would be helpful is to make this two dimensional. Imagine this:

[] [] [] [] []
[] [] [] [] []
[] [] [] [] []

This is what you are looking for. This is a 2D array which you can make like this. int[][] intArray = new int[3][5];

Now, you just have to keep track of where you are putting things carefully. You can put things into these positions on the array:

intArray[0][0]
intArray[0][1]
intArray[0][2]
intArray[0][3]
intArray[0][4]

But then as it is two dimensional, you can do this:

intArray[1][0]
intArray[1][1]

and so on.

The other way is to work with objects and create an object for your items, and then create an array of those objects.

I hope this helps,

0

My suggestion is you do a more object-oriented approach, expanding on awolfe91's answer. I'd suggest having a class Position, where you store info on how many checkers are in each position and the color of these. Then you only need an array of Position.

In the Position class you can for example have method canAddChecker(color) which for instance can return a boolean if it is valid to add a checker of that color to that position.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.