简体   繁体   中英

Returning a single int from int[] method in java

I am trying to make a method 'difference' that finds the largest number in an int array and returns the int to the main method. I am getting an error telling me I can't convert an int array to an int. How can I make this work? I don't want to return an entire array. Thanks a lot

public class bigDiff{
    public static void main(String[] args){
        int[] arr = new int[] {7, 6, 8, 9};
        difference(arr);
    }

    public static int difference (int[] input){
        int smallest = input[0];
        int largest = input[0];

        for (int i = 0; i < input.length; i++){
            if(input[i] >= largest){
                largest = input[i];
            } else if(input[i] <= input[largest]){
                smallest = input[i];
            }
        } 
        return largest;

    }   
}

watch this line of code

else if(input[i] <= input[largest]){
            smallest = input[i];
        }

input[largest] , first time largest value is 7 means input[7],and your array size is only 4.so this not work.

input[largest] instead use input[i] .

I'll try to explain a few things:
First, your declaration of the array is quite weird, you should declare: int[] arr = {7, 6, 8, 9};
Second, difference function returns an int, what are you doing with it? nothing.. try printing it.. System.out.println(difference(arr));
Third, you're trying to get the largest number, why do you need the smallest number? Even the compiler warns you for a variable which isn't being used. So your difference function should be like:

int largest = input[0];
for (int i = 0; i < input.length; i++){
    if(input[i] > largest)
        largest = input[i];
} 
return largest;

** You don't need to do anything if input[i] == largest , so checking if(input[i] > largest) is enough.

Good Luck :)

You have declared an array ie int[] arr = new int[] {7, 6, 8, 9} length of this array is 4 but here in the difference function,

for (int i = 0; i < input.length; i++){
        if(input[i] >= largest){
            largest = input[i];
        } else if(input[i] <= input[largest]){
            smallest = input[i];
        }
    } 

when you are comparing this in else if(input[i] <= input[largest]) then it will generate error because in the first iteration largest=7 and in next iteration it will go in else if where input[1]<=input[7] is checked so the array out of bound exception will be generated.

I think you want to get the largest element in the array so instead of doing this you can short the array and access the largest element easily.

Problems:
1. largest is used as an index (ie, input[largest] ), which may cause ArrayOutOfBoundsException if largest is larger than the array length 2. largest is used in the if to set smallest
3. difference returns the largest element instead of the difference between the largest and smallest elements
4. Return value of difference isn't used (not a real problem, but you can't see the output of difference )

Otherwise the (amended) program below works:

public class bigDiff{
    public static void main(String[] args){
        int[] arr = new int[] {7, 6, 8, 9};
        System.out.println(difference(arr)); // problem 4 was here
    }

    public static int difference (int[] input){
        int smallest = input[0];
        int largest = input[0];

        for (int i = 0; i < input.length; i++){
            if(input[i] >= largest){
                largest = input[i];
            } else if(input[i] <= smallest){ // problems 1 and 2 were here
                smallest = input[i];
            }
        } 
        return largest - smallest; // problem 3 was here

    }   
}

This should work just fine:

public static int difference (int[] input){
    int smallest = input[0];
    int largest = input[0];

    for (int i = 0; i < input.length; i++){
        if(input[i] > largest){
            largest = input[i];
        } else if(input[i] < smallest){
            smallest = input[i];
        }
    } 
    return largest;

}  

Also there is no point in keeping the smallest value when you're interested only in the largest.

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