java printing an array with Arrays.toString() error
Solution 1
method toString in class Object cannot be applied to given types. required: no arguments found: int[] reason: actual and formal argument list differ in length
May be you have a variable named Arrays
, that's why the compiler is complaining thinking that you are trying to invoke the Object.toString()
, which doesn't take any argument.
Try
System.out.println(java.util.Arrays.toString(totals));
Solution 2
The fact that it says "method toString in class Object cannot be applied to given types." makes me think you might not be importing the java.util.Arrays
class properly, or you have some other object called Arrays
.
Solution 3
This works for me:
int[] totals = {1,2};
System.out.println(Arrays.toString(totals));
printing:
[1, 2]
Are you sure you use at least JDK 5 ?
Indeed, Arrays.toString(int[] a)
only exists since JDK 5.
Related videos on Youtube
Levon Tamrazov
Updated on September 26, 2022Comments
-
Levon Tamrazov over 1 year
So I was trying to print an array of ints in my program, and following these instructions What's the simplest way to print a Java array?
wrote the following code:
int[] totals = //method that returns int array System.out.println(Arrays.toString(totals));
But it wont compile saying that
"method toString in class Object cannot be applied to given types. required: no arguments found: int[] reason: actual and formal argument list differ in length"
Why does it do that? Do I not pass my array as an argument to toString? If not, how do I use toString?
Thank you!
-
pb2q over 11 years
toString()
on an array won't do anything useful: it just prints the reference for the array. The OP wants to use theArrays
class to print each value. -
Sashi Kant over 11 yearsIf you try this for arrays: it will print the comma separated data
-
Lo Juego over 11 yearsMaybe you have a variable named Arrays? Common! My answer was downvoter for less :)
-
Levon Tamrazov over 11 yearsYeah I tried this as well before posting this question, it printed the memory address.
-
Levon Tamrazov over 11 yearsAs I just commented above I called the class for this program Arrays, so that's probably it. Thank you for your feedback!
-
Levon Tamrazov over 11 yearsAlso thank you for that workaround! I used it instead of having to rename the class. Worked like a charm!