我无法弄清楚如何在2D数组中找到数字的出现。我是java新手,因此我必须在不使用地图/高级解决方案的情况下执行此程序。
示例:
给定数组(由用户提供):
结果:
这是我到目前为止(我最难找到的方法是freqOccurrence !!!):
public static void main(String[] args) {
int i, j;
int[][] m = readDimension();
lerMatriz(m);
System.out.println(Arrays.deepToString(m));
freqOccurrence(m);
}
private static int[][] readDimension() {
Scanner in = new Scanner(System.in);
System.out.println("Number of rows? ");
int i = in.nextInt();
while (i < 1) {
System.out.println("Please define a valid number of rows. ");
i = in.nextInt();
}
System.out.println("Number of columns? ");
int j = in.nextInt();
while (j < 1) {
System.out.println("Please define a valid number of columns. ");
j = in.nextInt();
}
int[][] m = new int[i][j];
return m;
}
private static void readMatrix(int[][] m) {
Scanner in = new Scanner(System.in);
System.out.println("\nInput integers to save in the array:");
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
System.out.print("Element " + i + "," + j + ": ");
m[i][j] = in.nextInt();
}
}
}
private static double freqOccurrence(int[][] m) { //??????????????????
int num = 0;
int[][] m2 = new int[][];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
if (m[i] == num) {
m2[num]++;
}
}
}
}
我感谢所有的帮助!
答案 0 :(得分:0)
你有一个给定数组N行和M列。
让我们定义结果数组2行N M列(如果inisital数组中的所有数字都是唯一的N M就足够了)。
通过初始数组(按行和列循环2次)。对于每个值,调用方法
updateResult(int[][] result, int theNumber)
在方法中,通过结果数组查找结果中是否已存在数字。
如果找不到号码,只需在频率为1的末尾添加。
如果它已经存在,请增加频率并开始重新排序。假设您在结果数组中找到当前数字的索引是K.改变频率后与左侧单元格进行比较。如果新频率较大并且左邻居的小区交换它们并再次检查左邻居小区。继续,直到左侧单元格具有相同频率的单元格位于最左侧(0位置)。
答案 1 :(得分:0)
你可以这样做的一种方法(假设你被允许使用内置排序,并且你的数组中的每个数组都是相同的长度)只需几个步骤:
以下是代码:
private static int[][] getOccurrenceArray(int[][] input)
{
// create a new 1D array that we'll shove
// all indices of the 2D array into
int[] inputCopy = new int[input.length * input[0].length];
int index = 0;
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < input[0].length; j++) {
inputCopy[index] = input[i][j];
index++;
}
}
Arrays.sort(inputCopy);
// find the number of unique values in the 1D array - this will be the length of
//the first dimension of your frequency array
int uniqueValues = 1;
int previouslySeen = inputCopy[0];
for (int i = 1; i < inputCopy.length; i++) {
// if value at the current index isn't the same as the value
// at the previous index, we've found a new unique value
if (inputCopy[i] != previouslySeen) {
uniqueValues++;
}
previouslySeen = inputCopy[i];
}
// create the frequency array
int[][] frequencyArray = new int[uniqueValues][2];
previouslySeen = inputCopy[0];
int frequency = 0;
index = 0;
for (int i = 0; i < inputCopy.length; i++) {
// if we haven't reached a new value, increment the frequency
if (inputCopy[i] == previouslySeen) {
frequency++;
}
// if we've reached a new value, assign the previous totals to
// the array and reset frequency to 1
else {
frequencyArray[index][0] = previouslySeen;
frequencyArray[index][1] = frequency;
index++;
frequency = 1;
}
previouslySeen = inputCopy[i];
}
// don't forget to record the last value group
frequencyArray[frequencyArray.length - 1][0] = previouslySeen;
frequencyArray[frequencyArray.length - 1][1] = frequency;
return frequencyArray;
}
如果您要重用任何功能,其中一些功能应该被拉出到不同的方法中(特别是将2D数组折叠成一维数组的代码,以及找到数字的代码)排序的1D数组中的唯一项目。)