import java.util.*;
//Bunch of other stuff, including declaring a class
String[] aTest = {"one", "two", "three"};
aTest = java.util.Arrays.sort(aTest);
Whenever I compile this, I get:
src/JavaCheck.java:122: incompatible types
found : void
required: java.lang.String[]
aTest = java.util.Arrays.sort(aTest);
^
1 error
I don't get this. I have a java.lang.String[] as a parameter, yet it says
the parm is void.
So what's going on, and what am I overlooking or don't know about using
this?
Thanks!
Hal
> I'm trying to sort an array of strings. It won't compile, and I'm using
> something similar to examples. Here's what I have:
>
> import java.util.*;
>
> //Bunch of other stuff, including declaring a class
>
> String[] aTest = {"one", "two", "three"};
> aTest = java.util.Arrays.sort(aTest);
>
> Whenever I compile this, I get:
>
> src/JavaCheck.java:122: incompatible types
> found : void
> required: java.lang.String[]
> aTest = java.util.Arrays.sort(aTest);
> ^
> 1 error
>
> I don't get this. I have a java.lang.String[] as a parameter, yet it
says
> the parm is void.
<SNIP>
Actually, the compiler is trying to tell you that java.util.Arrays.sort()
returns void, but you are attempting to assign the result to aTest, which
requires java.lang.String[].
See the javadocs for the sort method of java.util.Arrays. The javadocs are
your friend!
Your line 122 should probably look like:
java.util.Arrays.sort(aTest);
Note that there is no assignment; this sort method receives an Array of
Object_s (that implement Comparable), and returns with the Array sorted in
ascending order.
(If you don't have the javadocs, download them from java.sun.com and
install them. Once you learn to use them, you will find them valuable.)
--
Ian Shef
These are my personal opinions and not those of my employer.
Hal Vaughan wrote:
> src/JavaCheck.java:122: incompatible types
> found : void
> required: java.lang.String[]
> aTest = java.util.Arrays.sort(aTest);
> ^
> 1 error
>
> I don't get this. I have a java.lang.String[] as a parameter, yet it says
> the parm is void.
The 'sort' method returns void, so you can't assign its result to the
String array. Try
String[] aTest = {"one", "two", "three"};
java.util.Arrays.sort(aTest);
instead.
Regards,
Remco
Actually, I have the JavaDocs. I had looked up "Sort" in the index, and was
reading the index. I just forgot to click on the link to see how it was
actually used. My bad.
Thanks for pointing it out!
Hal