Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to have an ArrayList as instance variable of an object?

Tags:

java

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?

like image 514
JDelage Avatar asked Dec 03 '22 13:12

JDelage


1 Answers

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;
    (...)} 
like image 179
Gregory Pakosz Avatar answered Apr 19 '23 22:04

Gregory Pakosz