简体   繁体   中英

Why do I keep getting this error when using Arrays.sort()?

This is the code:

import java.util.Arrays;
import java.util.*;
public class MyClass {
public static void main(String[] args) {
    int sum = 0;
    System.out.println("I will ask you to add 5 integers and I will add 5 to 
    each integer.\n");

    Scanner askInput = new Scanner(System.in);
    System.out.println("Please enter 5 integers:");

    int[] array = new int[5];
    array[0] = askInput.nextInt();
    array[1] = askInput.nextInt();
    array[2] = askInput.nextInt();
    array[3] = askInput.nextInt();
    array[4] = askInput.nextInt();

    System.out.println("\n");

    for(int i = 0; i < array.length; i++)
    {
        sum = array[i] + 5;
        System.out.println(sum);
    }

    System.out.println(Arrays.sort(array[0]));



}
}

And this is my error:

/MyClass.java:26: error: no suitable method found for sort(int) System.out.println(Arrays.sort(array[0]));

and:

/MyClass.java:26: error: 'void' type not allowed here System.out.println(Arrays.sort(array));

This error is when I remove the [0] from "(array)".

'void' type not allowed here System.out.println(Arrays.sort(array));

You can't print a void type.

You're receiving this error because Arrays.sort(array) sorts array in place ; that is to say, it does not return a new array of sorted items, it directly modifies the contents of the array you pass as as the argument.

You can instead do the following:

//Arrays.toString() returns a string representation of an array so you can print it
System.out.println(Arrays.toString(array)); 
//sort it
Arrays.sort(array);
//print the now sorted array
System.out.println(Arrays.toString(array)); 

Documentation: https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])

Also there is no need to import java.util.Arrays if you import java.util.* . This is because when you import java.util.* that automatically imports everything inside java.util, that would include java.util.Arrays, however to solve your problem you cannot print arrays directly. However, To convert an array to a String you can use Arrays.toString(array) . A good thing to note is that Arrays.sort(array) returns nothing and modifies the array directly, so you may want to clone your array beforehand, if you need the original for something else, to completely fix your code follow Strikegently's solution.

System.out.println(Arrays.sort(array[0]));

I think in the above line, you want to sort the array and then want to print the first value (at 0th index).

First, you need to understand that the return type of Arrays.sort() is void. Arrays.sort() method sort the values in the actual array.

You need to convert the above line in the following line.

Arrays.sort(array); // It sorts the array
System.out.println(array[0]); // It print the first value of the array

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