将数组中的相同值合并在一起

时间:2022-05-08 18:09:14
public static String[][][] cleanUp(String[][][] array) {
    for (int f = 0; f < array.length; f++) {
        for (int g = 0; g < array[f].length; g++) {
            int position = 0;
            //boolean flag = false;
            int count = 0;
            for (int h = 0; h < array[f][g].length; h++) {
                if (array[f][g][h].equals(array[f][g][h+1])) count++;
                else {
                    ArrayList<String> temp = new ArrayList<String>(Arrays.asList(array[f][g]));
                    for (int i = count - 1; i > position; i--) {
                        temp.remove(i);
                        position = i-1 ;
                    }
                    temp.set(position, array[f][g][h] + " (" + count + ")");
                }
            }
        }
    }
    return array;
}

Essentially, what I want to do is take a 3D array of strings and have each 1D array in it display quantity for repeated values. For instance, if I had an array of Strings like this:

基本上,我想要做的是采用一个3D数组的字符串,并在其中的每个1D数组显示重复值的数量。例如,如果我有一个这样的字符串数组:

[go, go, go, go, go, go]
[go, stop, stop, stop]

it would become:

它会变成:

[go (5)]
[go (1), stop (3)]

How could I do this, and what is it I am doing wrong?

我怎么能这样做,我做错了什么?

1 个解决方案

#1


5  

You need to change your last inner loop:

你需要改变你的最后一个内循环:

        int count = 0;
        for (int h = 0; h < array[f][g].length; h++) {
            if (array[f][g][h].equals(array[f][g][h+1])) count++;
            //You dont check for out of bound here, so `h + 1` will cause out of bound error
            else {
                ArrayList<String> temp = new ArrayList<String>(Arrays.asList(array[f][g]));
                for (int i = count - 1; i > position; i--) {
                    temp.remove(i);
                    position = i-1 ;
                }
                temp.set(position, array[f][g][h] + " (" + count + ")");
            }
            //Count is not reset after this, so this will be wrong!
        }

How I would do it:

我该怎么做:

        ArrayList<String> tmp  = new ArrayList<>();
        for (int h = 0; h < array[f][g].length; h++) {
            int count = 1;
            while(h + count < array[f][g].length && array[f][g][h].equals(array[f][g][h+count])) 
               count++;
            tmp.add(array[f][g][h] + "(" + count + ")");
            h += count - 1;//Update h to skip identical element
        }

ArrayList tmp will hold the result for array[f][g], and you should notice how I update h accordingly to skip all identical element.

ArrayList tmp将保存数组[f] [g]的结果,你应该注意我如何相应地更新h以跳过所有相同的元素。

Update: testing result

更新:测试结果

#1


5  

You need to change your last inner loop:

你需要改变你的最后一个内循环:

        int count = 0;
        for (int h = 0; h < array[f][g].length; h++) {
            if (array[f][g][h].equals(array[f][g][h+1])) count++;
            //You dont check for out of bound here, so `h + 1` will cause out of bound error
            else {
                ArrayList<String> temp = new ArrayList<String>(Arrays.asList(array[f][g]));
                for (int i = count - 1; i > position; i--) {
                    temp.remove(i);
                    position = i-1 ;
                }
                temp.set(position, array[f][g][h] + " (" + count + ")");
            }
            //Count is not reset after this, so this will be wrong!
        }

How I would do it:

我该怎么做:

        ArrayList<String> tmp  = new ArrayList<>();
        for (int h = 0; h < array[f][g].length; h++) {
            int count = 1;
            while(h + count < array[f][g].length && array[f][g][h].equals(array[f][g][h+count])) 
               count++;
            tmp.add(array[f][g][h] + "(" + count + ")");
            h += count - 1;//Update h to skip identical element
        }

ArrayList tmp will hold the result for array[f][g], and you should notice how I update h accordingly to skip all identical element.

ArrayList tmp将保存数组[f] [g]的结果,你应该注意我如何相应地更新h以跳过所有相同的元素。

Update: testing result

更新:测试结果