2015-10-05 62 views
-1

我在解决任务的最后部分时遇到了问题。我得到的最小数量的阵中,但是当我移动到另一个方法我得到这个在我的打印:如何将多维数组移动到另一个方法中?

[[[email protected] 

如果我写这在我的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; 
    } 
} 
+1

,我不明白你的问题。 “用不同的方法进行”是什么意思?你到目前为止尝试过什么?你也没有用数值填充你的数组。在你的'System.out.println'中,你应该使用'smallest'而不是'numbers [row] [column]'。 – Wavemaster

+0

编辑了这个问题,希望现在更清楚 –

+0

@KristofferNærland'getArray()'返回一个*数组*(!),然后打印出来。您如何期望打印单个号码? – Biffen

回答

1

您需要将阵列传递给方法。现在,你在你的getArray()方法中创建一个新的数组,它没有任何内容!

你必须明白的是,即使你的方法是Static,你的数组并不是!这意味着你需要将你的数组作为参数传递给你的方法。

此外,您得到该输出的原因是您将数组传递给您的打印语句,而不是调用getArray()方法(它也会返回此处的地址)。

在你的getArray()方法中,你实际上并不需要或者真的想创建第二个数组。这样想一想;如果你的数组是100万个元素呢?你是否真的想要分配另外的1,000,000个元素来寻找最小的元素?没有!这对系统资源非常重要。我们将遍历我们已经创建的数组!

基于上面介绍的问题,您需要返回找到的最小数字,而不是数组的地址!这意味着你需要你的返回类型更改为doublereturn smallest

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; 
     } 
    } 
0
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; 
    } 
} 
+1

如果你解释为什么他的代码不起作用以及为什么你的方法更好,而不是仅仅给出代码,那么对OP可能是有益的。 :) –

+2

请添加一个解释给你的答案,张贴未注释的代码可能没有多大帮助。 OP肯定有兴趣找出他做错了什么。 –

+0

好的。感谢您的关注 –

相关问题