我正在制作一个程序来检查井字游戏是否是胜利。 我一直得到一个IndexOutOfBoundsException
这是我的代码:
public class J1 {
public static void main(String[] args) {
J1 prog = new J1();
prog.run(args);
}
ArrayList<Integer> board = new ArrayList<Integer>();
int[] checkPattern = {0, 1, 2, 3, 6};
void run(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Integer> board = new ArrayList<Integer>();
for(int i = 0; i < 3; i++){
String[] line = in.nextLine().split(" ");
for(String x : line){
if(x.equals("X")){
board.add(0);
} else if(x.equals("O")){
board.add(1);
} else {
board.add(-1);
}
}
}
System.out.println(board);
for(int x : checkPattern){
if(checkPos2(x)){
System.out.println("YES");
return;
}
}
System.out.println("NO");
}
boolean checkPos2(int index){
int current = board.get(index);
return false;
}
此行发生错误:
int current = board.get(index);
在调用board
之前,我的checkPos2(x)
数组似乎有元素,但是当我调用它时它们就消失了。我试过四处寻找但无济于事。任何有关正在发生的事情的见解都非常感谢。感谢
答案 0 :(得分:3)
在run
方法中,你有这一行:
ArrayList<Integer> board = new ArrayList<Integer>();
它声明了一个名为board
的局部变量,它掩盖了您的类变量board
。您在run
内所做的所有更改只会影响该本地值,而字段board
不会受到影响。然后checkPos2
尝试从字段中读取一个值,该值仍为空。
完全删除run
中的声明(因为该字段已经完全初始化),问题将得到解决。
答案 1 :(得分:0)
实际上没有理由使用ArrayList。 对于我的学校,我正在写一本关于这个主题的考试 我只是得到了一些非常好的代码,我会在这里为你发布并解释它。
#include "stdio.h"
void call( int * i ){
int iValue = *i; //retrieve value saved in pointer
int iResult = iValue * 1000; //any calc
*i = iResult; //save new value in pointer
}
int main(){
int i = 123;
printf("initial value: %d\n", i);
call( &i );
printf("value after: %d\n", i);
return 0;
}
这是我的田野课(德语&#34; Feld&#34;)。你应该重命名它。 每个领域都有一个价值。 inrange检查值是否为-1,0,1(可以用于teamA,没有,teamB(例如).Negate只是更改字段的值(例如1到-1)。如果要创建一个KI,你需要克隆这个对象(如果你想知道如何在TIC TAC TOE中写一个KI,我可以向你解释一下(给我发一封电子邮件给finn.eggers@gmx.de)
My Seccond类是我的实际数组:
public class Feld implements Cloneable{
private int value; //0 = kein Stein 1 = Team weiß 2 = Team schwarz (0 by default)
public Feld() {
}
public Feld(int v) {
this.setValue(v);
}
public int getValue() {
return value;
}
public void negate(){
value = -value;
}
public void setValueWithoutRangeCheck(int value){
this.value = value;
}
public void setValue(int value) {
if(inrange(value))
this.value = value;
}
private boolean inrange(int value) {
return (value >= -1 && value <= 1);
}
public Feld clone() {
return new Feld(value);
}
@Override
public String toString() {
return "Feld{" + "value=" + value + '}';
}
}
这只是创建一个数组,它不可能获得数组之外的任何值。所以不能抛出任何例外)
我也实现了一个迭代器。 如果你不知道它是什么,我只是解释一下:
您可以通过调用:youArray.iterator()来创建新的迭代器。 这将从FeldArrayIterator类型返回一个新的Iterator 这个迭代器有一个字段,通过调用getCurrent(),你得到了实际的 数组的元素,迭代器指向。 通过调用next()。它转到Array中的下一个元素。 因此,当您想获得数组的每个元素时,您只需要调用:
public class FeldArray implements Cloneable{
private Feld[][] felder;
public class FeldArrayIterator{
private FeldArray array;
private int currentX;
private int currentY;
protected FeldArrayIterator(FeldArray array,int x, int y){
this.array = array;
this.currentX = x;
this.currentY = y;
}
public void goTo(int x, int y){
this.currentX = x;
this.currentY = y;
}
public void next(int x, int y) {
this.currentX = this.currentX + x;
this.currentY = this.currentY + y;
}
public int getCurrentX() {
return this.currentX;
}
public int getCurrentY() {
return this.currentY;
}
public void next() {
this.currentX ++;
if(this.getCurrent() == null){
this.currentX = 0;
this.currentY ++;
}
}
public Feld getCurrent() {
return array.getFeld(currentX, currentY);
}
}
public FeldArrayIterator iterator() {
return new FeldArrayIterator(this,0,0);
}
public FeldArray(int size){
this.resetFeld(size);
}
public int getAmountOf(int i){
int lsg = 0;
FeldArrayIterator it = this.iterator();
while(it.getCurrent() != null){
if(it.getCurrent().getValue() == i){
lsg ++;
}
it.next();
}
return lsg;
}
public int getSize() {
return this.felder.length;
}
private void resetFeld(int size) {
felder = new Feld[size][size];
for(int i = 0; i < this.getSize(); i++){
for(int n = 0; n < this.getSize(); n++){
this.setFeld(new Feld(), i, n);
}
}
}
private Feld getFeld(int x, int y) {
if(inrange(x,y))
return this.felder[x][y];
else{
return null;
}
}
private void setFeld(Feld f, int x, int y){
if(inrange(x,y))
this.felder[x][y] = f;
}
public void setValue(int value,int x, int y){
if(inrange(x,y))
this.getFeld(x, y).setValue(value);
}
public int getValue(int x, int y) {
if(inrange(x,y)){
return this.getFeld(x, y).getValue();
}
else{
return 0;
}
}
public FeldArray clone() {
FeldArray array = new FeldArray(this.getSize());
for(int i = 0; i < this.getSize(); i++){
for(int n = 0; n < this.getSize(); n++){
array.setFeld(this.getFeld(i, n).clone(), i, n);
}
}
return array;
}
public String toString() {
String res = "FeldArray\n";
for(int i = 0; i < this.getSize(); i++){
for(int n = 0; n < this.getSize(); n++){
res += this.getValue(n,i)+" ";
}
res += "\n";
}
res += "-----------------";
return res;
}
private boolean inrange(int x, int y) {
return (x >= 0 && y >= 0 && x < this.getSize() && y < this.getSize());
}
}
实际上它很容易退出。但永远不要忘记it.next()。
通过调用it.next(x,y)。你可以去任何相对于你的位置的元素。 所以如果你当前的位置是(1,2)而你说的是。下一个(2,2)。你现在的位置是(3,4)。
我希望我能帮助你。