Java多维数组和整数引用
我有一个名为numList的整数数组Java多维数组和整数引用,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有一个名为numList的整数数组 [4, 4, 3, 3, 3, 2, 1, 1, 1, 1, -1, -12, -12, -12, -12] 我想创建一个多维数组,它可以存储 哪个左侧表示数字,哪个右侧确定出现的次数 我试过的尝试。。。我什么也没得到 // Declaring the new multi-dimensional array. int [] [] newArray = new int [6] [2]; // Counter 3. i
[4, 4, 3, 3, 3, 2, 1, 1, 1, 1, -1, -12, -12, -12, -12]
我想创建一个多维数组,它可以存储
哪个左侧表示数字,哪个右侧确定出现的次数
我试过的尝试。。。我什么也没得到
// Declaring the new multi-dimensional array.
int [] [] newArray = new int [6] [2];
// Counter 3.
int counter3 = 0;
// Get first occurrence.
while (numList[counter3] < numList.length){
for (int counter3:numList){
newArray[] ([counter3]++);
}
这应该行得通。如果仍然需要返回数组,请使用以下命令:
private int[][] segregateArray(int[]list) {
Map<Integer, Integer> resultHelper = new HashMap<>();
for (int i : list) {
if (resultHelper.containsKey(i)) {
resultHelper.put(i, resultHelper.get(i) + 1);
} else {
resultHelper.put(i, 1);
}
}
int[][] result = new int[resultHelper.size()][2];
int arrayIterator=0;
for(Integer key : resultHelper.keySet())
{
result[arrayIterator][0]=key;
result[arrayIterator][1]=resultHelper.get(key);
arrayIterator++;
}
return result;
}
在实际项目中,您可能应该避免自己使用低级阵列机制实现这样的功能—您添加了一个广泛的测试套件,不是吗?:选择一个可用的库 在Java8中,使用闭包可以很好地实现这一点,类似于这里描述的内容:
在Java 7和更早的版本中,我会使用一个集合库,例如Guava,它包含一个集合,提供您想要的内容。假设您的数字与示例numList中的数字一样有序,那么您可以执行以下操作:
int[] numList = { 4, 4, 3, 3, 3, 2, 1, 1, 1, 1, -1, -12, -12, -12, -12 };
int[][] newArray = new int[6][2];
int index = 0;
for (int i = 0; i < numList.length;) {
int count = 0;
for (int x = 0; x < numList.length; x++)
if (numList[x] == numList[i]) count++;
newArray[index][0] = numList[i];
newArray[index][1] = count;
index++;
i += count;
}
for (int x = 0; x < newArray.length; x++) {
for (int i = 0; i < newArray[0].length; i++)
System.out.print(newArray[x][i] + " ");
System.out.println();
}
这样,您就不必像其他答案中那样处理导入问题,而且这会更短,但这只有在您订购了数字的情况下才有效。不过,还有一些很好的排序算法
编辑:我更改了它,以便它可以接受任何大小的任何顺序的数字
int[] numList = { 6, 6, 5, 5, 4, 4, 3, 2, 1, 1, 1, 7, 6, 5, 7, 8, 65, 65, 7 };
int[][] newArray = new int[1][2];
int index = 0;
for (int i = 0; i < numList.length;) {
try {
int count = 0;
boolean isUnique = true;
for (int x = 0; x < i; x++)
if (numList[x] == numList[i]) {
isUnique = false;
break;
}
if (isUnique) {
for (int x = 0; x < numList.length; x++)
if (numList[x] == numList[i]) count++;
newArray[index][0] = numList[i];
newArray[index][1] = count;
index++;
}
i++;
} catch (ArrayIndexOutOfBoundsException e) {
int tmpArray[][] = newArray;
newArray = new int[tmpArray.length + 1][tmpArray[0].length];
for (int row = 0; row < tmpArray.length; row++)
for (int col = 0; col < 2; col++)
newArray[row][col] = tmpArray[row][col];
}
}
for (int x = 0; x < newArray.length; x++) {
for (int i = 0; i < newArray[0].length; i++)
System.out.print(newArray[x][i] + " ");
System.out.println();
}
因此,在这一点上,使用另一个答案的地图可能会更短。我的第二个答案的唯一好处是不用担心进口。好的,你有什么尝试吗?很好的故事,兄弟。您的问题在哪里?请发布您最近独立解决此问题的失败尝试。已更新。。。我设法把它分类,然后我就被绊倒了我需要导入什么吗?导入java.util.HashMap;导入java.util.Map;谢谢你的努力。自从你发帖以来,我一直在努力。作为一个新手,这是我应该用来调用你的方法吗?我确实更改了int[]numList,对于int I:numListin main methodor,只要调用这个方法int[]array={4,4,3,3,2,1,1,1,-1,-12,-12};int[][]newArray=arrayarray;我做了一个System.out.printlnArrays.toString-Warray;[[I@63ce0e18, [I@6cff7cd8, [I@795d80cf, [I@69b3d448, [I@1d35f92f, [I@427a8ba4]已返回。这是因为这是newArray中每个数组的标识符。我更新了代码以正确显示它。感谢它现在起作用。太棒了!我正在尝试分析并从中学习一些东西。
int[] numList = { 6, 6, 5, 5, 4, 4, 3, 2, 1, 1, 1, 7, 6, 5, 7, 8, 65, 65, 7 };
int[][] newArray = new int[1][2];
int index = 0;
for (int i = 0; i < numList.length;) {
try {
int count = 0;
boolean isUnique = true;
for (int x = 0; x < i; x++)
if (numList[x] == numList[i]) {
isUnique = false;
break;
}
if (isUnique) {
for (int x = 0; x < numList.length; x++)
if (numList[x] == numList[i]) count++;
newArray[index][0] = numList[i];
newArray[index][1] = count;
index++;
}
i++;
} catch (ArrayIndexOutOfBoundsException e) {
int tmpArray[][] = newArray;
newArray = new int[tmpArray.length + 1][tmpArray[0].length];
for (int row = 0; row < tmpArray.length; row++)
for (int col = 0; col < 2; col++)
newArray[row][col] = tmpArray[row][col];
}
}
for (int x = 0; x < newArray.length; x++) {
for (int i = 0; i < newArray[0].length; i++)
System.out.print(newArray[x][i] + " ");
System.out.println();
}