Java 如何在二维数组中查找和计数重复项?
嗨,我正在尝试通过一个二维数组(特别是一个4x4数组),找到任何重复的数字,然后计算数字重复的次数。到目前为止,我有4个for循环可以工作,但是做的比我真正想要的更多Java 如何在二维数组中查找和计数重复项?,java,arrays,for-loop,Java,Arrays,For Loop,嗨,我正在尝试通过一个二维数组(特别是一个4x4数组),找到任何重复的数字,然后计算数字重复的次数。到目前为止,我有4个for循环可以工作,但是做的比我真正想要的更多 int counter1 =1; String huh=""; for (int x = 0; x< dataTable.length; x++) { for (int y=0; y< dataTable.length; y++) { f
int counter1 =1;
String huh="";
for (int x = 0; x< dataTable.length; x++)
{
for (int y=0; y< dataTable.length; y++)
{
for (int z = 0; z< dataTable.length; z++)
{
for (int a=0; a< dataTable.length; a++)
{
if ( x != z && x !=a && y != z && y !=a)
{
if (dataTable[x][y] == dataTable[z][a])
{
counter1++;
}
}
}
}
if (counter1 > 1)
{
huh += ("\n " + dataTable[x][y] + " repeats " + counter1 + " times!");
}
counter1=1;
}
}
但是,按照我的设置的工作方式,每次比较数组中每个位置的数字3时,它都会将相同的语句添加到字符串中。那么,我的方法是正确的,只需要一些调整吗?还是完全错了,我需要完全不同的东西?我在大学里只上过初级编程课,所以到目前为止我们只知道java的基础知识,比如数组、循环和其他一些东西 我认为最好的方法是维护一个跟踪数字频率的
Map
(即,它将数组中的每个数字映射到它出现的次数)。在整个数组中循环并相应地更新此映射并不困难。在我看来,你现在所做的事情似乎比实际需要的要复杂得多
为什么要用4来表示循环?也许我误解了特定代码的用途,但在2D阵列上循环(并最终计算频率数)只需要两个:
相关文件:
映射,然后将其打印出来,如下所示:
public static void main(String[] args) throws Exception {
final int[][] dataTable = new int[][] {
new int[] {0, 1, 2, 1},
new int[] {0, 1, 3, 1},
new int[] {0, 1, 2, 2},
new int[] {0, 1, 2, 0}
};
final Map<Integer, Integer> map = new HashMap<Integer, Integer> ();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
final int value = dataTable[i][j];
final Integer currentCount = map.get(value);
final Integer newCount;
if (currentCount == null) {
newCount = 1;
}
else {
newCount = currentCount + 1;
}
map.put (value, newCount);
}
}
for (final Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println(String.format ("The number %d repeats %d times", entry.getKey(), entry.getValue()));
}
}
publicstaticvoidmain(字符串[]args)引发异常{
最终整数[][]数据表=新整数[][]{
新的int[]{0,1,2,1},
新的int[]{0,1,3,1},
新的int[]{0,1,2,2},
新的int[]{0,1,2,0}
};
final-Map=new-HashMap();
对于(int i=0;i<4;i++){
对于(int j=0;j<4;j++){
最终整数值=数据表[i][j];
最终整数currentCount=map.get(值);
最终整数newCount;
如果(currentCount==null){
newCount=1;
}
否则{
newCount=currentCount+1;
}
map.put(值,newCount);
}
}
对于(最终Map.Entry:Map.entrySet()){
System.out.println(String.format(“数字%d重复%d次”),entry.getKey(),entry.getValue());
}
}
您可以找到结果。最普遍的解决方案是使用地图,正如其他人所建议的那样。但是,如果数组值在相对较小的范围内,则可以使用数组而不是贴图。如果min
是(最多)数组中的最小值,而max
是(至少)最大值:
public int[] getFrequencyMap(int[][] array, int min, int max) {
int[] map = new int[max - min + 1];
for (int[] row : array) {
for (int val : row) {
map[val - min]++;
}
}
return map;
}
在返回的数组中,索引val-min
处的值将是值val
在数组中出现的次数。可以有一个n*n行和2列的数组:
/*being n the number of rows/columns*/
int count[]][] = new int[n*n][2];
for (int i = 0; i < dataTable.length; i++) {
for (int k = 0; k < dataTable.length; k++) {
/*this loop finds the position in which it should go*/
for (int h = 0; h < n*n; h++) {
if (count[h][0] == dataTable[i][k]) {
break;
}
/*Assuming that '0' is not a possible number in dataTable, use '-1' or a number that */
if (count[h][0] == 0) {
break;
}
}
count[h][0] = dataTable[i][k];
count[h][1]++;
}
}
/*为n行/列的数量*/
整数计数[]][]=新整数[n*n][2];
对于(int i=0;i
可能的重复项您知道Map
s吗?您可以有一个Map
来跟踪条目的频率。Map解决方案为+1。因为它是一个四维数组,而不是一个二维数组。@MikeJohnson你怎么知道的?好吧,从这个问题来看,我猜-“嗨,我想通过一个二维数组(特别是4x4数组)”。我想Impalaguy指的是四维数组(不是4x4=16维,这将是很多维:-D)我想他可能指的是一个4行4列的2D数组(例如int[4][4]
)。哦,对了,我的错。所以它只是两个循环,而不是四个+答案是1
public int[] getFrequencyMap(int[][] array, int min, int max) {
int[] map = new int[max - min + 1];
for (int[] row : array) {
for (int val : row) {
map[val - min]++;
}
}
return map;
}
/*being n the number of rows/columns*/
int count[]][] = new int[n*n][2];
for (int i = 0; i < dataTable.length; i++) {
for (int k = 0; k < dataTable.length; k++) {
/*this loop finds the position in which it should go*/
for (int h = 0; h < n*n; h++) {
if (count[h][0] == dataTable[i][k]) {
break;
}
/*Assuming that '0' is not a possible number in dataTable, use '-1' or a number that */
if (count[h][0] == 0) {
break;
}
}
count[h][0] = dataTable[i][k];
count[h][1]++;
}
}