Need help saving specific values ​​from a 2-dimensional array

So I have a 2 dimensional array that looks like this

    {1, 20}
    {1, 14}
    {2, 15}
    {2, 67}
    {3, 55}
    {3, 24} 
    {4, 95}
    {4, 23}

What I need to do is find the largest number in the second column corresponding to each number, so for 1 the largest number is 20, for 2 the largest number is 67, etc. So, I tried using a while loop, which looks like this

int count = 0;
int count2 = 0;

int[][] array = {1, 20}
                {1, 14}
                {2, 15}
                {2, 67}
                {3, 55}
                {3, 24} 
                {4, 95}
                {4, 23}
int[] storeMax = new int[4]

while (array[0][count] < array[count].length)
            {
                if (array[count][1] > storeMax[count2])
                {
                    storeMax[count2] = degPerc[count][1];
                    count2++;
                }
                else
                {
                    count++;
                    count2++;
                }
            }

I have been in this for a while, but I do not seem to understand this. If anyone can help me, that will be awesome!

+4
source share
3 answers

I think one solution is slightly different from your solution.

Map<Integer, Integer> map. ( , , , ). atlast

public static void main(String[] args) {
    int[][] array = { { 1, 20 }, { 1, 14 }, { 2, 15 }, { 2, 67 },
            { 3, 55 }, { 3, 24 }, { 4, 95 }, { 4, 23 } };

    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    System.out.println(array.length);
    for(int i=0;i<array.length; i++)
    {
        if(map.get(array[i][0]) == null)
                map.put(array[i][0], array[i][1]);
        else
            if(map.get(array[i][0]) < array[i][1])
            {
                map.put(array[i][0], array[i][1]);
            }
    }
    System.out.println(map);
}
+1

Java 8, Map.compute :

static int[][] array = { { 1, 20 }, { 1, 14 },
    { 2, 15 }, { 2, 67 }, { 3, 55 },
    { 3, 24 }, { 4, 95 }, { 4, 23 } };

public static void main(String args[]) {
    Map<Integer, Integer> m = new HashMap<>();
    for (int i = 0; i < array.length; i++) {
        int value = array[i][1];
        int key= array[i][0];
        m.compute(key, (k, v) -> v == null ? value : Math.max(v, value));
    }
}
+1

-, , ,

int[][] array = {{1, 20},
            {1, 14},
            {2, 15},
            {2, 67},
            {3, 55},
            {3, 24},
            {4, 95},
            {4, 23}};

, , , java. , . , - , .

while (array[0][count] < array[count].length)

, , . , , - , (2- ) (1- ). .

int count = 0;
while(count < array.length){
  count++;
}

, 0 1,

int count = 0;
while(count < array.length){
  int keyVal = array[count][0];
  int numVal = array[count][1];
  count++;
}

Finally, your sample data has only possible values ​​for keys 1-4. If all of this ever happens, it's 1-4, then your storeMax variable will work to store the results. Otherwise, you will want to use the map, as Prashant says in the comments above.

So, if the keys will always be between 1-4, you can perform a check in a loop and save them.

while(count < array.length){
    int keyVal = array[count][0];
    int numVal = array[count][1];

    if(storeMax[keyVal-1] < numVal){
        storeMax[keyVal-1] = numVal;
    }
    count++;
}

Remember that arrays are based on 0. That's why you should use keyVal-1.

0
source

All Articles