简体   繁体   中英

Trying to count number of items in a 2d array

I am trying to find a comparable value that is common to all rows in a 2D array. For that value, I'd like to find the minimal (> 0) number of repetitions that exist in all rows.

For example, when working with a 2D array of String:

{
 {A, C, B},
 {A, A, B},
 {C, D, A} 
}

The only value that exists in all rows is "A". The minimal number of appearances in a row is 1, so the answer would be 1 A .

Here is my code: I am trying to search each column in a row for duplicates (or triplets, etc.), determine the count for a given row and compare it to the other rows to determine the row with the lowest quantity. Also, maybe there is a more elegant approach? For some reason it is not working (Collections is a 2d String array):

public class CommonElements {
    ArrayList<String> commonCollections = new ArrayList<String>();

    private int comparisons = 0;
    int i, j, k;
    int count, lowestCount;
    String previousString = "";
    int row[];
    String current;

    public Comparable[] findCommonElements(Comparable[][] collections) {

        Arrays.sort(collections[0]);

        row = new int[collections[0].length];

        for (i = 0; i < collections[0].length; i++) { // first row column selection
            current = collections[0][i].toString();
            lowestCount = 1;
            for (j = 0; j < collections.length; j++) { // row
                count = 0;
                for (k = 0; k < collections[0].length; k++) { // column
                    if (current.equals(collections[j][k].toString())) { // if contains same string as first row column selected
                        count++;
                        System.out.print(count + "\n");
                    }
                }
                if (lowestCount < count) {
                    lowestCount = count;
                }
            }
        }

        System.out.print(lowestCount);

        return collections[0];
    }

    public int getComparisons() {
        return comparisons;
    }


}

Well, first you take collections[0][i].toString() when i is 0 so that evaluates to A , then program goes through all those loops and lowestCount is set to 1 . Then your first for loop moves on to B and lowestCount is reseted without it being saved anywhere. You should save your lowestCount perhaps in array or a list, and at the end of the first for loop (after 2 other for loops) add the lowestCount to that array and you will have the lowest count of every letter. If you don't want to save it you can just System.out.println("Lowest count of letter: "+current+" is: "+lowestCount); . If you want to determine the row with the lowest count you can also save it in array (row with lowest count for every letter) and set it in that if that if statement passes ( if(lowestCount < count) ).

I'm not sure if I understood you correctly, but there is definitely a better approach to solving this problem.

You can do like this

int[][] arr = new int[5][2];
    int count =0;
    for(int[] i : arr){
        count = count + i.length;
    }
    System.out.println(count);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM