-1
   int[] arr = new int[4];
   Arrays.fill(arr, 4);
   Arrays.stream(arr).forEach(System.out::println);

I was solving a problem and few of the methods were not recognized. I felt that posting the entire code wasn't necessary so I created a program for those specific methods. They were still unrecognized.

As far as I can imagine, this can be an issue with my vs code. The code works fine in other directories. Any suggestions?

Java version: java 18.0.1.1

I have tried cleaning the workspace but the issue persists in the directory.

Akil
  • 145
  • 4
  • 6
    You have *another* Arrays class in your directory which has priority over `java.lang.Arrays` when you use simple name Arrays. To resolve this conflict either rename your *other* Arrays class, or use full package name `java.util.Arrays.fill(..)` (same about `stream`). – Pshemo Jul 12 '22 at 14:17
  • This question should include more details and clarify the problem. – Roman C Jul 12 '22 at 14:29

1 Answers1

5

You have a naming conflict. You have a class called Arrays in the same package which conflicts with the java.util.Arrays class you're trying to use.

Get rid of the import statement and use the fully qualified class name instead:

java.util.Arrays.fill(arr, 4);

Or use a fully qualified import:

import java.util.Arrays;
// ...
Arrays.fill(arr, 4);

Or even a static import if you're into that sort of thing:

import static java.util.Arrays.fill;
// ...
fill(arr, 4);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • 4
    This is also another reason to avoid the `import java.util.*;`, as `import java.util.Arrays;` would have correctly resolved the methods. – Rogue Jul 12 '22 at 14:19
  • And, I was blaming VS Code for no reason. Thank you for pointing out the conflict. That Arrays.java is actually empty. I must have created it while using quick fix of VS code. Well, I guess I was indeed blaming VS code for a reason. – Akil Jul 12 '22 at 15:39