如何在java中比较整数2D数组的ArrayList和int import java.util.Random; 公共类突变{ 公共突变() { } private ArrayList allWords=new ArrayList(); 公共数组列表字生成器(int noOfWords) { int rand,j; int-word[][]=新int[8][8]; 对于(int i=0;i
而言,问题在于2D数组初始化 如果如何在java中比较整数2D数组的ArrayList和int import java.util.Random; 公共类突变{ 公共突变() { } private ArrayList allWords=new ArrayList(); 公共数组列表字生成器(int noOfWords) { int rand,j; int-word[][]=新int[8][8]; 对于(int i=0;i,java,arraylist,int,Java,Arraylist,Int,而言,问题在于2D数组初始化 如果noOfWords=1,2D数组的值是多少 import java.util.Random; public class Mutation { public Mutation() { } private ArrayList<int[][]> allWords=new ArrayList<int[][]>(); public ArrayList<int[][]> wordGenrator(int noOfWords
noOfWords=1
,2D数组的值是多少
import java.util.Random;
public class Mutation {
public Mutation()
{
}
private ArrayList<int[][]> allWords=new ArrayList<int[][]>();
public ArrayList<int[][]> wordGenrator(int noOfWords)
{
int rand,j;
int word[][]=new int[8][8];
for(int i=0;i<noOfWords;i++)
{
for(j=0;j<8;j++)
{
rand = new Random().nextInt(2);
System.out.print(rand);
word[i][j]=rand;
}
System.out.print("\n");
allWords.add(word);
}
return allWords;
}
public boolean isExistWordOfAllOnes()
{
int counter;
for(int i=0;i<5;i++)
{
counter=0;
for(int j=0;j<8;j++)
{
if(equals(this.allWords.get(i)[i][j]==1)
{
counter++;
}
}
if(counter==8)
{
return true;
}
}
return false;
}
int-word[][]=new-int[8][8];
对于(int i=0;i就我阅读您的代码和文本而言,我认为您有一个int[5][8]数组,并且您希望检查数组的每一行中是否至少有一个“1”。在这种情况下,您需要按以下方式更改代码
public boolean isExistWordOfAllOnes()
{
int counter;
for(int i=0;i<5;i++) // iterate all the words that are 2D arrays
{
counter=0;
for(int j=0;j<8;j++) // row
{
for (int k = 0; k < 8; k++) { // column
if (this.allWords.get(i)[j][k]==1) { // if it is equal to 1
counter++;
}
}
}
if(counter==8*8)//total 64 ones in 8*8 2D array
{
return true;
}
}
return false;
}
公共布尔值isExistWordOfAllOnes()
{
整数计数器;
对于(int i=0;iThat;应该是a)
if(等于(this.allWords.get(i)[i][j]==1)
此外,==已经在比较两个事物,因此“equals”不是。去掉equals。
这应不适用于以下情况:
添加(word)
更改后,将只有一个单词[],因此,将i更改为1:
这个.allWords.get(1)
在这个循环中,你到底在做什么?我不知道这些符号或数字的一半来自哪里。你想实现什么?所有数字都是在同一类的其他函数中随机生成的。然后如果返回总是真的。
public boolean isExistWordOfAllOnes()
{
int counter;
for(int i=0;i<5;i++) // iterate all the words that are 2D arrays
{
counter=0;
for(int j=0;j<8;j++) // row
{
for (int k = 0; k < 8; k++) { // column
if (this.allWords.get(i)[j][k]==1) { // if it is equal to 1
counter++;
}
}
}
if(counter==8*8)//total 64 ones in 8*8 2D array
{
return true;
}
}
return false;
}
public boolean isExistWordOfAllOnes()
{
int counter;
for(int i=0;i<5;i++)
{
counter=0;
for(int j=0;j<8;j++)
{
if(this.allWords.get(i)[i][j]==1)
{
counter++;
}
}
if(counter < 1) // If there were no 1's then count should still be 0
{ // and the method returns false as a row was found
return false; // in which there wasn't a 1
}
}
return true;
}