Cannot find symbol Java error?

后端 未结 3 459
轮回少年
轮回少年 2021-01-12 00:55

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

import java.util.Arrays.*;

class         


        
相关标签:
3条回答
  • 2021-01-12 01:28

    you need to do a static import. Use the following

    import static java.util.Arrays.*;

    Reason

    when you want to import some static members (methods or variables), you need to static import the members. So you have to use import static

    Another solution

    or you can import

    import java.util.Arrays;
    

    and use

    Arrays.sort(b);
    

    Reason of the second Solution

    here you are not importing any static elements so normal import to Arrays is needed. Then you can directly access using Arrays.sort

    0 讨论(0)
  • 2021-01-12 01:35

    You are attempting to do a static import, but you missed static.

    //   add v this
    import static java.util.Arrays.*;
    
    0 讨论(0)
  • 2021-01-12 01:36

    It's a static method of the class Arrays.

    You should invoke it like this:

    Arrays.sort(someArray);
    

    Note you still have to import the Arrays class like this:

    import java.util.Arrays;
    

    Or as others have mentioned, if you do a static import you can omit the class name.

    I would argue that Arrays.sort() is better for readability.

    0 讨论(0)
提交回复
热议问题