在Java 2D数组中查找最大值和最小值 [英] Finding minimum and maximum in Java 2D array

查看:71
本文介绍了在Java 2D数组中查找最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在试图解决这个问题,需要一些帮助.我需要找到最小/最大值并将其打印出来以用于多维数组.这是我尝试过的两种方法.

I have been trying to figure this out for a while and need some help. I need to find the min/max values and print them out for a multidimensional array. Here are the two ways that I have tried.

import java.util.*;

class MinMax {
    public static void main(String[] args) {
        int[][] data = {{3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}};
        Arrays.sort(data);
        System.out.println("Minimum = " + data[0]);
        System.out.println("Maximum = " + data[data.length - 1]);
    }
}

此版本符合要求,但无法运行.

This version complies but doesn't run.

import java.util.*;

class MinMax {
    public static void main(String[] args) {
        int[][] data = {{3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}};

    public static int getMaxValue(int[] numbers) {
        int maxValue = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > maxValue) {
                maxValue = numbers[i];
            }
            return maxValue;
            {
                public static int getMinValue (int[] numbers) {
                    int minValue = numbers[0];
                    for (int i = 1; i < numbers.length; i++) {
                        if (numbers[i] < minValue) {
                            minValue = numbers[i];
                        }
                    }
                return minValue;
            }

此版本只会在编译时引发很多错误.任何帮助,我们将不胜感激.

This version just throws me a bunch of errors in compiling. Any help is greatly appreciated.

推荐答案

好的,我已经修复了您的代码.实际上,您的错误是您没有遍历多维数组的所有单元.

Ok, I've kinda fixed your code. Actually your mistake was that you have not been traversing all the cells of your multidimensional array.

因此,我在getMinValue/getMinValue方法和固定数组元素寻址中添加了附加循环.

So, I've added additional loop into getMinValue/getMinValue methods and fixed array elements addressing.

import java.util.*;

class MinMax {
    public static void main(String[] args) {
        int[][] data = {
                {3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}
        };
        System.out.println(getMaxValue(data));
        System.out.println(getMinValue(data));
    }


    public static int getMaxValue(int[][] numbers) {
        int maxValue = numbers[0][0];
        for (int j = 0; j < numbers.length; j++) {
            for (int i = 0; i < numbers[j].length; i++) {
                if (numbers[j][i] > maxValue) {
                    maxValue = numbers[j][i];
                }
            }
        }
        return maxValue;
    }

    public static int getMinValue(int[][] numbers) {
        int minValue = numbers[0][0];
        for (int j = 0; j < numbers.length; j++) {
            for (int i = 0; i < numbers[j].length; i++) {
                if (numbers[j][i] < minValue ) {
                    minValue = numbers[j][i];
                }
            }
        }
        return minValue ;
    }
}

这篇关于在Java 2D数组中查找最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆