Проблема с заполнение массива Arraylist
Уже была похожая проблема Ошибка заполнение коллекции ArrayList методом .add , но снова не понимаю в чем дело.
Есть пара классов:
/**
* Основной класс, с инициализацией полей.
*/
public class Main {
Scanner scanner;
PlayerManager playerManager;
public static void main(String[] args) {
Main start = new Main();
PlayerManager manager = new PlayerManager();
Count count = new Count(0);
manager.addPlayer(start.playerInvitation(), count);
manager.addPlayer(start.playerInvitation(), count);
System.out.println(count);
System.out.println(manager.players);
System.out.println(manager.players.get(0).id);
System.out.println(manager.players.get(1).name);
start.runGame();
}
void runGame() {
Field plate = new Field();
plate.fillTheField(); //инициализация поля отображаемого для игроков
plate.fillTheSpace(); //поле для вычислений
plate.printFields();
// mainCycle(plate);
}
/**
* Основной цикл игры между 2 пользователями
*/
//
// void mainCycle(Field plate) {
// do {
//
//
// } while (field.space.size() != 0);
// }
/**
* приглашения пользователя
*/
String playerInvitation() {
System.out.println("Введите имя игрока: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
return name;
}
}
Другой класс:
/**
* класс игровое поле
*/
public class Field {
char[][] field; //видимое поле 1
ArrayList<Integer> space; //альтернативное внутреннее поле 2
Field() {
char[][] field = new char[3][3]; // 1
this.field = field;
ArrayList<Integer> space = new ArrayList<Integer>(9);
this.space = space; // 2
}
void fillTheField() { // 1
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
this.field[i][j] = '.';
}
}
}
void fillTheSpace() {
// for (Integer counter : space) {
// counter++;
// }
for (int i = 0; i < 9; i++) {
space.add(i);
}
}
void printFields() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(this.field[i][j] + "\t\t");
}
System.out.println("");
System.out.println("");
}
for (int i = 0; i < 9; i++) {
System.out.print(space + "\t");
}
}
void markX(int i, int j) {
this.field[i][j] = 'X';
}
void markO(int i, int j) {
this.field[i][j] = '0';
}
}
В результате выполнения вот этой строки
plate.fillTheSpace(); (метод runGame/класс Main)
хочу получить массив цифр, по типу : [1], [2], [3], [4] .....
А получаю : [0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, ......
Вроде массив создан и проинициализирован. Что не так с методом fillThePlace? Т.е. как заполнить список из 9-ти элементов 9-ю цифрами?
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
for (int i = 0; i < 9; i++) {
System.out.print(space.get(i) + "\t");
^^^^^^^
}