如何在数组中查找重复项的索引(Java)
对于这个编程任务,我们应该为数独游戏的一行找到这个数组中重复项的索引。我有这个方法,静态布尔等于(int[]a,int[]a2):如何在数组中查找重复项的索引(Java),java,arrays,for-loop,if-statement,while-loop,Java,Arrays,For Loop,If Statement,While Loop,对于这个编程任务,我们应该为数独游戏的一行找到这个数组中重复项的索引。我有这个方法,静态布尔等于(int[]a,int[]a2): boolean isValue=true; int[]dataRow={9,8,7,6,5,4,3,2,8}; int[]chk={9,8,7,6,5,4,3,1,8}; isValue=Arrays.equals(dataRow,chk); int i,j; 对于(i=0;i
boolean isValue=true;
int[]dataRow={9,8,7,6,5,4,3,2,8};
int[]chk={9,8,7,6,5,4,3,1,8};
isValue=Arrays.equals(dataRow,chk);
int i,j;
对于(i=0;i
这里的这个程序只是打印出这样的内容:
Duplicate-9在索引0和0中找到,这意味着没有找到重复项。我评论说8在索引8和1中找到。我只是不确定如何打印找到重复项的位置。我尝试修改if语句,但没有成功:if(dataRow[I]!=chk[j])
,if(dataRow[i]==chk[i])
。我还尝试将嵌套的for循环放入while循环:while(!isValue)
,但这也不起作用。我想我的教授还想确保数组中的所有值都在1-9之间,我的想法是这样的:while(!isValue&&(dataRow>=1&&dataRow由于输入数组中的值范围有限,因此可以创建一个小型的检查数组来计算一个数字在输入数组中出现的次数,从而检测重复值和缺失值:
重复和缺失的公共静态无效检查(int…arr){
System.out.println(“输入:“+Arrays.toString(arr));
int[]check=new int[10];//用0填充
布尔noDuplicates=true;
对于(int i=0;i
测试:
输出:
Input: [9, 8, 7, 6, 5, 4, 3, 2, 8]
Duplicate value 8 found at index 8
Missing value: 1
-------
Input: [9, 8, 7, 6, 5, 4, 1, 3, 2]
No duplicates found in the input array
All digits present in the input array
-------
Input: [9, 8, 7, 6, 5, 1, 2]
No duplicates found in the input array
Missing value: 3
Missing value: 4
-------
更新
数组check
可以在输入数组中存储数字索引(移位1),然后可以打印关于第一个索引的信息:
/。。。
对于(int i=0;i
第一个输入数组的输出:
Input: [9, 8, 7, 6, 5, 4, 3, 2, 8]
Duplicate value 8 found at index 8, first value is at 1
Missing value: 1
由于输入数组中的值范围有限,因此可以创建一个小型的检查数组来计算一个数字在输入数组中出现的次数,从而检测重复值和缺失值:
重复和缺失的公共静态无效检查(int…arr){
System.out.println(“输入:“+Arrays.toString(arr));
int[]check=new int[10];//用0填充
布尔noDuplicates=true;
对于(int i=0;i
测试:
输出:
Input: [9, 8, 7, 6, 5, 4, 3, 2, 8]
Duplicate value 8 found at index 8
Missing value: 1
-------
Input: [9, 8, 7, 6, 5, 4, 1, 3, 2]
No duplicates found in the input array
All digits present in the input array
-------
Input: [9, 8, 7, 6, 5, 1, 2]
No duplicates found in the input array
Missing value: 3
Missing value: 4
-------
更新
数组check
可以在输入数组中存储数字索引(移位1),然后可以打印关于第一个索引的信息:
/。。。
对于(int i=0;i
第一个输入数组的输出:
Input: [9, 8, 7, 6, 5, 4, 3, 2, 8]
Duplicate value 8 found at index 8, first value is at 1
Missing value: 1