简体   繁体   English

在方法中将数组作为参数传递

[英]Passing array as parameter in a method

I am trying to make a simple Chess program in java.我正在尝试用 Java 制作一个简单的国际象棋程序。 There is an ArrayList that stores all the moves the knight can do, with each move being an Integer[] .有一个 ArrayList 存储骑士可以做的所有移动,每个移动都是一个Integer[] The first item in the array is the row, and the second is the column.数组中的第一项是行,第二项是列。 It looks like this:它看起来像这样:

ArrayList<Integer[]> moves = new ArrayList<>();
moves.add({row - 2, col - 1});
moves.add({row - 1, col - 2});
moves.add({row - 2, col + 1});
moves.add({row - 1, col + 2});
moves.add({row + 1, col - 2});
moves.add({row + 2, col - 1});
moves.add({row + 2, col + 1});
moves.add({row + 1, col + 2});

For some reason, when I try to run the code, I get around 100 errors including illegal start of expression , <identifier> expected , and not a statement .出于某种原因,当我尝试运行代码时,我遇到了大约 100 个错误,包括illegal start of expression<identifier> expectednot a statement

I'm not sure what's causing the problem.我不确定是什么导致了问题。 Does Java not allow storing arrays in ArrayLists, or is there something wrong with the particular syntax? Java 不允许在 ArrayLists 中存储数组,还是特定语法有问题?

You always need to initialize the array for this to work.您总是需要初始化数组才能使其工作。

ArrayList<Integer[]> moves = new ArrayList<>();
moves.add(new Integer[]{ 2, 1});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM