我试图弄清楚如何创建一种方法来确保2d数组中的每个数字都是唯一的并且与其他任何数字都不相同。这应该有点简单,因为我的数字只能是1-9,并且只有9个,但是我仍然不明白如何不做类似的事情就能够以简单的方式找出答案
如果(array [0] [0] == array [0] [1] || array [0] [0] == array [0] [2])
,以此类推,直到每个数字array[0][0] == array[2][2]
。我会有一个非常长的嵌套的if语句组,用于9个数字,并且我怀疑我的教授打算让我们这样做。
那么我该如何使用循环来做我想在这里做的事情?我已经在StackOverflow上搜索了其他java 2d数组唯一算法,但是我找不到一个想要按照我为分配设置的规则将每个数字与其他数字进行测试的算法。请记住,我需要针对其他每个数字进行测试。
到目前为止,这是我的代码:
class MagicSquare
{
//2d array data member
private int[][] array = {{}};
/**
Constructor: Initializes 2d array.
@param arr The array.
*/
public MagicSquare(int[][] arr)
{
array = arr;
}
/**
This method displays all values from the elements
in the array.
*/
public void showArray()
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
System.out.println(array[row][col]);
}
}
}
/**
showResult for later
*/
public void showResult()
{
}
/**
This method determines if every number in the array
is in the range of 1-9.
@return The value of range.
*/
private boolean isInRange()
{
boolean range = false;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (array[row][col] == 1 || array[row][col] == 2 || array[row][col] == 3 ||
array[row][col] == 4 || array[row][col] == 5 || array[row][col] == 6 ||
array[row][col] == 7 || array[row][col] == 8 || array[row][col] == 9)
{
range = true;
}
else
{
range = false;
break;
}
}
}
return range;
}
/**
*/
private boolean isUnique()
{
boolean unique = false;
}
}
答案 0 :(得分:1)
将所有数字放入集合中,并使其与数组和集合的大小匹配。如果两者相等,则数组中的所有数字都是唯一的。让我知道您是否需要代码演示。
编辑1:
尝试此代码,
public static void main(String[] args) throws Exception {
Integer[] numbers = new Integer[] { 2, 3, 1, 7, 4, 6, 5, 11 };
Set<Integer> numberSet = new HashSet<Integer>(Arrays.asList(numbers));
if (numbers.length == numberSet.size()) {
System.out.println("Numbers in array are unique");
} else {
System.out.println("Numbers in array are not unique");
}
// for 2d array
Set<Integer> number2dSet = new HashSet<Integer>();
Integer[][] numbers2d = new Integer[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int numbers2dSize = 0;
for (Integer[] num2d : numbers2d) {
List<Integer> numberList = Arrays.asList(num2d);
number2dSet.addAll(numberList);
numbers2dSize += numberList.size();
}
if (numbers2dSize == number2dSet.size()) {
System.out.println("Numbers in 2d array are unique");
} else {
System.out.println("Numbers in 2d array are not unique");
}
}
答案 1 :(得分:1)
您可以使用java-8轻松完成此操作
int a[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int distinctValues = Arrays.stream(a).flatMapToInt(IntStream::of).distinct().count();
对于上面的示例,如果数组中的每个元素都不同,则值应为9。如果不不同,则值将小于9。
这将适用于任何n维数组。
答案 2 :(得分:0)
如果确保您的元素仅在1到arr.length * arr [0] .length的范围内,则可以这样做(对任何数组长度通用):
private boolean isEveryElementUnique( int[][] arr ) {
boolean[] isOccupied = new boolean[arr.length*arr[0].length+1];
for ( int i = 0; i < arr.length; i++ ) {
for ( int j = 0; j < arr[0].length; j++ ) {
if ( isOccupied[arr[i][j]] ) return false;
isOccupied[arr[i][j]] = true;
}
}
return true;
}
您不需要为此构建哈希集。它有不必要的开销。对于这种情况,一个简单的布尔数组就足够了。
答案 3 :(得分:0)
自
我的电话号码只能是1-9
替换此大代码:
.jar
使用
if (array[row][col] == 1 || array[row][col] == 2 || array[row][col] == 3 ||
array[row][col] == 4 || array[row][col] == 5 || array[row][col] == 6 ||
array[row][col] == 7 || array[row][col] == 8 || array[row][col] == 9)
然后
按照注释中的建议使用HashSet,检查数组的大小,即if (array[row][col] >= 1 && array[row][col] <= 9)
等于set的大小。如果重复输入,集合的大小肯定会变小。