Java 如何将多维数组移动到另一个方法中?

Java 如何将多维数组移动到另一个方法中?,java,arrays,methods,Java,Arrays,Methods,我无法解决任务的最后一部分。我得到的是数组中最小的数字,但当我将其移动到另一个方法时,我在打印中得到: [[D@2a139a55 如果我在我的main(String[]args){中写这个,我得到了正确的0 public class Task05 { public static void main(String[] args) { double[][] numbers = getArray(); System.out.println("Smalles

我无法解决任务的最后一部分。我得到的是数组中最小的数字,但当我将其移动到另一个方法时,我在打印中得到:

[[D@2a139a55
如果我在我的
main(String[]args){
中写这个,我得到了正确的0

public class Task05 {

    public static void main(String[] args) {
        double[][] numbers = getArray();

        System.out.println("Smallest number in array is " + numbers);

    }

    public static double[][] getArray() {

        double[][] numbers = new double[25][25];
        double smallest = Integer.MAX_VALUE;

        for (int row = 0; row < numbers.length; row++) {
            for (int column = 0; column < numbers[row].length; column++) {

                if (smallest > numbers[row][column]) {
                    smallest = numbers[row][column];



                }

            }
        }
        return numbers;
    }
}
公共类任务05{
公共静态void main(字符串[]args){
双[][]个数字=getArray();
System.out.println(“数组中最小的数字是”+数字);
}
公共静态双精度[][]getArray(){
双精度[]数字=新双精度[25][25];
双最小值=整数最大值;
for(int row=0;row数字[行][列]){
最小=数字[行][列];
}
}
}
返回号码;
}
}

您需要将数组传递给方法。现在,您正在
getArray()
方法中创建一个新数组,该数组中没有任何内容

您必须了解的是,即使您的方法是静态的,但数组不是!这意味着您需要将数组作为参数传递到方法中

另外,获得该输出的原因是将数组传递给print语句,而不是调用getArray()方法(此时也会返回地址)

getArray()中
method,您实际上不需要或确实不想创建第二个数组。请这样考虑;如果您的数组是1000000个元素,该怎么办?您真的想再分配1000000个元素以查找最小的元素吗?不!这是对系统资源的极大定价。我们将只需遍历我们拥有的数组alr已经创造了

根据我上面提到的问题,您需要返回找到的最小数字,而不是数组的地址!这意味着您需要将返回类型更改为
double
返回最小的

 public class Task05 {

        public static void main(String[] args) {
            double[][] numbers = getArray();

            System.out.println("Smallest number in array is " + getArray(numbers));

        }

        // Changed return type to double, instead of double[][]
        public static double getArray(double [][] numbers) {
            double smallest = Integer.MAX_VALUE;

            for (int row = 0; row < numbers.length; row++) {
                for (int column = 0; column < numbers[row].length; column++) {

                    if (smallest > numbers[row][column]) {
                        smallest = numbers[row][column];
                    }

                }
            }
            // Return the smallest number that you found
            return smallest;
        }
    }
公共类任务05{
公共静态void main(字符串[]args){
双[][]个数字=getArray();
System.out.println(“数组中最小的数字是”+getArray(数字));
}
//将返回类型更改为double,而不是double[]
公共静态双getArray(双[][]个数字){
双最小值=整数最大值;
for(int row=0;row数字[行][列]){
最小=数字[行][列];
}
}
}
//返回找到的最小数字
返回最小;
}
}
公共类JavaApplication8{
公共静态void main(字符串[]args){
//创建并初始化数组
int[]x={{1,2,0},{4,5,6};
//调用最小的方法并打印返回的值
System.out.println(“最小值为”+最小值(x));
}
//最小法
//查找数组中的小元素
公共静态int最小值(int[][]x){
//假设第一个元素是小元素
int最小值=x[0][0];
//迭代通过数组
对于(int i=0;i
我不理解你的问题。你所说的“以不同的方法执行”到底是什么意思?到目前为止你尝试了什么?而且你没有用值填充数组。在
System.out.println
中,你应该使用
最小的
而不是
数字[行][列]
。编辑了问题,希望更清楚now@KristofferNærland
getArray()
返回一个数组(!),然后打印。你希望如何打印一个数字?@Kristofernærland请记住接受你认为已经回答了你的问题的答案!这有助于后来遇到同样问题的其他人。如果你解释为什么他的代码不起作用以及为什么你的方法更好,而不仅仅是givi,这可能对OP有益ng代码:)请对您的答案进行解释,发布未注释的代码可能不会有多大帮助。OP肯定有兴趣找出他做错了什么。[System.out.println(“数组中最小的数字是”+数字);],这里您打印了数组的引用,而不是数组。我发布此方法是为了向您展示如何仅返回一个值。另一件事是,您声明了两个数组,一个在方法main中,另一个在getArray()方法中。
public class JavaApplication8 {

    public static void main(String[] args) {
        // create and initialize array
        int[][] x = {{1, 2, 0}, {4, 5, 6}};
        // call smallest method and print the value returned
        System.out.println("smallest is " + smallest(x));
    }

    // smallest method 
    // to find small element in array

    public static int smallest(int[][] x) {
        // suppose the first element is the small one
        int smallest = x[0][0];

        // iterate throgh array
        for (int i = 0; i < x.length; i++) {

            for (int j = 0; j < x[i].length; j++) {

                // compare current element with smallest element
                // if current element is the small one then save it in smallest variable
                if (x[i][j] < smallest) {
                    smallest = x[i][j];
                }
            }
        }
        // return smallest
        return smallest;
    }
}