1

I was given the following problem in class today.

Print (show result) of the following arrays:

int[ ] a = {2,4,7,10};
int[ ] b = Arrays.fill(a,7);
int[ ] c = Arrays.fill(a,1,3,8);

When i punch these lines of code into NetBeans, I get the following error:

"incompatible types - required: int[] - found: void"

Any ideas on what is not working correctly?

1
  • 1
    Have a look at the Javadoc and it will be quite clear what's going on. Btw, Javadoc is quickly accessible in Netbeans via rightclick or alt-f1 on any standard classname or method. Learn to use the Javadoc properly, it will save you lots of time and frustration. Commented Oct 7, 2013 at 21:21

2 Answers 2

6

Arrays#fill() doesn't return the array. It has void return type. Just use:

int[] a = {2,4,7,10};
Arrays.fill(a,7);
Arrays.fill(a,1,3,8);
Sign up to request clarification or add additional context in comments.

2 Comments

Should i be getting the following outputs after each line if i use a println after each of the three statements? [2, 4, 7, 10] [7, 7, 7, 7] [7, 8, 8, 7]
@user2856289 Yes. You can print the array after each fill, to see the difference.
5

Arrays#fill has a void return type, simply use

Arrays.fill(a,7);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.