简体   繁体   中英

error with returning a value from a method in java

hello in the code below I am sorting (sort method) a small array to find the largest number. I then print the answer in the (display method).

But to extend my knowledge I want to mass the max value back to them main in a return statement and then print from there....simply to learn how to return a value.

package christmas;

public class maxvalue 
{ 
    public static void main(String[] args) 
    { 
        int[] data={10,90,30}; 
        sort(data); 
        System.out.println("\nmax number is :"); 
        display(data); 
        System.out.println(data);
    } 
    static int display(int num[]) 
    { 
        System.out.print(num[0] + " "); 
        return num[0];
    }
    static void sort(int num[]) 
    { 
        int i, j, temp; 
        for(i=0; i<num.length-i;i++) 
        {  
            for(j=0; j<num.length-i-1;j++) 
            { 
                if(num[j]<num[j+1]) 
                { 
                    temp = num[j]; 
                    num[j] = num[j+1]; 
                    num[j+1] = temp; 
                }
            }
        }
    }
}

the output is:

max number is : 90 [I@4617c264

90 is the max value as printed by the display value. But after this I have a return of the max value and then I try and print the return. But instead of an integer it looks like a memory location is being printed.

Any ideas please - I am student but this is not homework - simply trying to catch up. I appreciate that there are more elegant ways to calculate the max value in an array but what I am trying to learn is the passing of arguments to and from a method.

原因是您试图在最后一个 System.out 中打印数据,它是一个数组,这就是您看到内存地址的原因。尝试打印 display(data),您将看到输出所需的数字。

Instead of printing the returned value, you are printing the data array memory location:

System.out.println(data);

You should change that line with:

System.out.println(display(data));

With that line we have:

  1. Your display method is called and it prints the max value
  2. Your display method returns the max value
  3. println takes that returned value and prints it

try System.out.println(data[0]); data is your array therefore printing data without an index will only print its memory location

private static int sort(int[] array){
  int a, b, max = 0;
  for (int i = 1;//If i was 0, it would have thrown an ArrayIndexOutOfBoundsException.
      i < array.length; i++){
    a = array[i-1];//The first element in the array.
    b = array[i];//The second one, and so on.
    if (a > b) max = a;//Check the bigger number.
    else max = b;
  }
  return max;
}
private static void display(int nr){
  System.out.print(nr);//Or what you want to do with the number.
}
public static void main(String[] args){
  int[] data={10,90,30};
  display(sort(data));
  //Or do it like this.
  int max = sort(data);
  display(max);
}

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