4

I'm working on a class project to build a little Connect4 game in Java. My current thinking is to have a class of Columns that have as instance variable a few integers (index, max. length, isFull?) and one ArrayList to receive both the integers above and the plays of each players (e.g., 1's and 0's standing for X's and O's). This is probably going to be split between 2 classes but the question remains the same.

My current attempt looks like this:

import java.util.ArrayList;
public class Conn4Col {
    public int hMax; 
    public int index;
    public final int initialSize = 0;
    public final int fullCol = 0;
    public ArrayList<Integer>;
    (...)}  

Unfortunately, this doesn't compile. The compiler says an <identifier> is missing where my ArrayList declaration stands.

We're just starting objects and we haven't really looked into other instance variables than the basic types.

Can someone tell me where my error is and how to correct it?

Coderino Javarino
  • 2,819
  • 4
  • 21
  • 43
JDelage
  • 13,036
  • 23
  • 78
  • 112

5 Answers5

13

You forgot to give your member a name.

import java.util.ArrayList;
public class Conn4Col {
    public int hMax; 
    public int index;
    public final int initialSize = 0;
    public final int fullCol = 0;
    public ArrayList<Integer> list;
    (...)} 
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
2
public ArrayList<Integer> list;

But, do not declare ArrayList public:

private ArrayList<Integer> list = new ArrayList<Integer> ();

public List<Integer> getList ()
{
    return Collections.unmodifiableList(list);
}
Bob
  • 61
  • 1
  • 6
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • 1
    This is good practice, but doesn't answer the question does it? He was missing a name for the variable. – Sean Owen Mar 25 '10 at 15:25
  • This is by the way not the root cause. See Gregory's answer for the real answer. You're simply forgotten the identifier, like as you have done for `hMax`, `index`, etc..etc.. – BalusC Mar 25 '10 at 15:26
2

One more addition: better use java.util.List and only use the specific ArrayList during the creation of the object:

public List<Integer> list;
...
list = new ArrayList<Integer>();

That way you can change the actual implementation without having to change the member declaration.

pitpod
  • 389
  • 1
  • 3
0
private List<Integer> list = new ArrayList<Integer>();
Robby Pond
  • 73,164
  • 16
  • 126
  • 119
0

You forgot to give the variable a name. Correct Syntax:

public ArrayList<Integer> listName;
Anushil Kumar
  • 674
  • 8
  • 8