简体   繁体   中英

Find a location of a value on a 2d array in java

I have input values for my 2d array, which is 5 by 2 in size. I have occurences of each number on one column. and each number on the other column

I have found the max value of occurrences, and need to move to a cell left of the value in the 2d array to print the value of number according to the occurrences. Any help will be great!

My Codes:

import java.util.Collections;
import java.util.Scanner;
import java.util.Arrays;
import java.util.List;

public class Code {

    public static void main(String[] args) {
        int num1, num2, num3, num4, num5, sum, avg, max, min;

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the first number:");
        num1 = keyboard.nextInt();
        System.out.println("Enter the seond number:");
        num2 = keyboard.nextInt();
        System.out.println("Enter the third number:");
        num3 = keyboard.nextInt();
        System.out.println("Enter the fourth number:");
        num4 = keyboard.nextInt();
        System.out.println("Enter the fifth number:");
        num5 = keyboard.nextInt();
        keyboard.close();

        int[] num = new int[5]; // array named num is declared with 5 variables
        num[0] = num1;
        num[1] = num2;
        num[2] = num3;
        num[3] = num4;
        num[4] = num5;
        Arrays.sort(num);

        int n1 = num[0];
        int n2 = num[1];
        int n3 = num[2];
        int n4 = num[3];
        int n5 = num[4];

        List<Integer> index1 = Arrays.asList(n1, n2, n3, n4, n5);
        int occurrences1 = Collections.frequency(index1, n1);

        List<Integer> index2 = Arrays.asList(n1, n2, n3, n4, n5);
        int occurrences2 = Collections.frequency(index2, n2);

        List<Integer> index3 = Arrays.asList(n1, n2, n3, n4, n5);
        int occurrences3 = Collections.frequency(index3, n3);

        List<Integer> index4 = Arrays.asList(n1, n2, n3, n4, n5);
        int occurrences4 = Collections.frequency(index4, n4);

        List<Integer> index5 = Arrays.asList(n1, n2, n3, n4, n5);
        int occurrences5 = Collections.frequency(index5, n5);

        int[][] nums = new int[5][2];
        nums[0][0] = n1;
        nums[1][0] = n2;
        nums[2][0] = n3;
        nums[3][0] = n4;
        nums[4][0] = n5;
        nums[0][1] = occurrences1;
        nums[1][1] = occurrences2;
        nums[2][1] = occurrences3;
        nums[3][1] = occurrences4;
        nums[4][1] = occurrences5;

        int maxOccurrences = Math.max(Math.max(Math.max(Math.max(occurrences1, occurrences2), occurrences3), occurrences4), occurrences5);
        System.out.println(maxOccurrences);

        sum = (int) (num1 + num2 + num3 + num4 + num5);
        avg = (int) (sum / 5);
        max = Math.max(Math.max(Math.max(Math.max(num1, num2), num3), num4), num5);
        min = Math.min(Math.min(Math.min(Math.min(num1, num2), num3), num4), num5);

        System.out.println(" ");
        System.out.println("Sum:" + sum);
        System.out.println("Avg:" + avg);
        System.out.println("Max:" + max);
        System.out.println("Min:" + min);
        System.out.println("Median:" + num[2]);
        System.out.println("Mode:" + min);

    }
}

I assume that a 2D array is not the most elegant data structure for what you want to achieve. It looks like a TreeMap could be appropriate here, but that's just a guess.

In any case, READ THESE RESOURCES :

Imagine you had to extend your current code to support more than 5 input values!

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Occurances
{
    public static void main(String[] args)
    {
        int numNums = 5;

        int[][] num = new int[numNums][2];

        Scanner keyboard = new Scanner(System.in);
        for (int i=0; i<numNums; i++)
        {
            System.out.println("Enter the "+i+"th number:");
            num[i][0] = keyboard.nextInt();
            //num[i][0] = (i*i)%numNums;
            //System.out.println("Number "+i+" is "+num[i][0]);
        }
        keyboard.close();

        sort(num, 0);

        int maxOccurrences = -1;
        for (int i=0; i<numNums; i++)
        {
            num[i][1] = computeFrequency(num[i][0], num, 0);

            maxOccurrences = Math.max(maxOccurrences, num[i][1]);
        }

        System.out.println("maxOccurrences "+maxOccurrences);

        int sum = computeSum(num, 0);
        int avg = sum / numNums;
        int max = computeMax(num, 0);
        int min = computeMin(num, 0);

        System.out.println(" ");
        System.out.println("Sum:" + sum);
        System.out.println("Avg:" + avg);
        System.out.println("Max:" + max);
        System.out.println("Min:" + min);
        System.out.println("Median:" + num[numNums/2][0]);
        System.out.println("Mode:" + min);
    }

    private static int computeSum(int array[][], int column)
    {
        int sum = 0;
        for (int i=0; i<array.length; i++)
        {
            sum += array[i][column];
        }
        return sum;
    }

    private static int computeMin(int array[][], int column)
    {
        int min = Integer.MAX_VALUE;
        for (int i=0; i<array.length; i++)
        {
            min = Math.min(min, array[i][column]);
        }
        return min;
    }

    private static int computeMax(int array[][], int column)
    {
        int max = Integer.MIN_VALUE;
        for (int i=0; i<array.length; i++)
        {
            max = Math.max(max, array[i][column]);
        }
        return max;
    }

    private static void sort(int array[][], final int column)
    {
        Arrays.sort(array, new Comparator<int[]>()
        {
            @Override
            public int compare(int[] a0, int[] a1)
            {
                return Integer.compare(a0[column], a1[column]);
            }
        });
    }

    private static int computeFrequency(int value, int array[][], int column)
    {
        int count = 0;
        for (int i=0; i<array.length; i++)
        {
            if (array[i][column] == value)
            {
                count++;
            }
        }
        return 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