2

I made a board class that is simply an array with 3 integers.

public class board
{
   public int[] boardArray = new int [3];

   public board (int[] b1)
   {
      for(int i=0;i<b1.length;i++)
      {
         boardArray[i] = b1[i];
      }
   }

}

I want to print out all the boards added to an array list called losers, but I can only get it to print the spot in memory. I understand that I have to run a loop and call each individual piece of the array up, but I do not understand how to access the elements of my array. Here is the code I use to print:

for (board b : loser)
{
  System.out.println( b.int[0] +"" + b.int[1]+"" + b.int[2]);
}
3
  • 2
    Its b.boardArray[0] not b.int[0]. Commented Jan 29, 2016 at 5:12
  • Great! Thank you so much. Commented Jan 29, 2016 at 5:14
  • Note that a Java Array is not the same thing as an ArrayList - the latter is a List backed by an array. Commented Jan 29, 2016 at 5:21

4 Answers 4

1

all the boards added to an array list called losers

for (board b : loser)
  {
  System.out.println( b.boardArray[0] +"" + b.boardArray[1]+"" + b.boardArray[2]);
  }

Since boardArray is an type ofint in board class. But int is an datatype not a variable. Just replace b.int[index] with b.boardArray[index].

Sign up to request clarification or add additional context in comments.

Comments

1

This System.out.println( b.int[0] +"" + b.int[1]+"" + b.int[2]); should be something like

System.out.println( b.boardArray[0] +" " + b.boardArray[1]
        + " " + b.boardArray[2]);

or override toString in board (which by Java naming conventions, should start with a capital letter Board). Also, you might use Arrays.toString(int[]) like

@Override
public String toString() {
    return Arrays.toString(boardArray);
}

then you could call System.out.println(b); Finally, an int[] (array of integers) is not an ArrayList (a generic List implementation that is backed by, but distinct from, an array).

Comments

0

your array name is boardArray of type int, so fields or variables are accessed by name not by type. replace int by boardArray

System.out.println( b.boardArray[0] +"" + b.boardArray[1]+"" + b.boardArray[2]);

Comments

0

You should use b.boardArray[] since boardArray is the member of board class. int is the datatype of boardArray, not a variable.

import java.util.*;

class board
{
   public int[] boardArray = new int [3];

   public board (int[] b1)
   {
      for(int i=0;i<b1.length;i++)
      {
         boardArray[i] = b1[i];
      }
   }

}

public class Solution
{
    public static void main(String[] args) {

        ArrayList<board>loser=new ArrayList<>();

        int[] a1 = {10,20,30};
        int[] a2 = {80,90,91};

        loser.add(new board(a1));
        loser.add(new board(a2));

        for(board b : loser)
        {
            System.out.println( b.boardArray[0] +" " + b.boardArray[1]+" " + b.boardArray[2]);
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.