检查Array Java中的重复
如何检查我是否在Array中有重复?
答案 0 :(得分:1)
我假设您想逐行检查,所以这里是我使用的功能。
public static boolean isValidSodukoRow (int[][] board, int i)
{
int[][] Soduko = board;
int[] row = new int[Soduko.length];
int counter=0;
boolean ans=true;
for(int j=0;j<Soduko.length;j++)
{
row[j]=Soduko[i][j];
System.out.print(""+Soduko[i][j]);
}
for(int j=1;j<=row.length;j++)
{
for(int k=0;k<row.length;k++)
{
if( row[k]>row.length || row[k]<1)
{
ans=false;
return ans;
}
if(j==row[k] )
{
counter=counter+1;
}
}
if(counter>=2 || counter==0)
{
ans=false;
return false;
}
counter=0;
}
return ans;
}
答案 1 :(得分:0)
假设我理解正确,你可以试试这个:
Integer[] a = {1,2,3,2,4}; // some array
System.out.println(new HashSet<Integer>(Arrays.asList(a)).size() == a.length);
通过转换为Set
我们删除重复项,因此只有当此集的大小不等于原始数组的大小时才存在重复项。