Can't find the Java error symbol?

The code works when I used java.util.Arrays.sort(numbers); Am I doing something wrong? It seems strange to me.

 import java.util.Arrays.*; class Test { public static void main(String[] args) { double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; char[] chars = {'a', 'A', '4', 'F', 'D', 'P'}; sort(numbers); System.out.println(binarySearch(numbers, 3)); } } 

(The error is displayed in the terminal)

 Test.java:8: error: cannot find symbol sort(numbers); ^ symbol: method sort(double[]) location: class Test Test.java:10: error: cannot find symbol System.out.println(binarySearch(numbers, 3)); ^ symbol: method binarySearch(double[],int) location: class Test 2 errors 
+4
source share
3 answers

This is a static method of the Arrays class.

You should call it like this:

 Arrays.sort(someArray); 

Note that you still need to import the array class as follows:

 import java.util.Arrays; 

Or, as others have already noted, if you are performing a static import, you can omit the class name.

I would say that Arrays.sort() better read.

+12
source

you need to do a static import. Use the following

import static java.util.Arrays.*;

Cause

when you want to import some static members (methods or variables), you need to statically import the elements. Therefore you should use import static

Another solution

or you can import

 import java.util.Arrays; 

and use

 Arrays.sort(b); 

The reason for the second decision

you are not importing any static elements here, so normal import into arrays is necessary. Then you can directly access using Arrays.sort

+5
source

You are trying to perform a static import, but you have missed static .

 // add v this import static java.util.Arrays.*; 
+2
source

Source: https://habr.com/ru/post/1482634/


All Articles