-2

Below is the start of a method I am trying to write. But java doesn't like my use of Arrays.sort method

It says that I have an,

Incompatible type: required int[], found void.

Can anyone see where I am going wrong?

static String [] findSpace(int lengthOfName1, int lengthOfName2, int lengthOfName3){
        String [] space = {":", ":", ":"};
        int [] lengthOfName = {0,0,0};
        int [] lengthOfNameSorted = {0, 0, 0};
        lengthOfName[0] = lengthOfName1; lengthOfName[1] = lengthOfName2; lengthOfName[2] = lengthOfName3;
        lengthOfNameSorted = Arrays.sort(lengthOfName);
warl0ck
  • 2,804
  • 2
  • 24
  • 46
jay
  • 5
  • 3

2 Answers2

1

Arrays.sort() returns void. It sorts the array in-place, hence destroying the initial order.

Make sure that's what you need.

You can use

int[] lengthOfNameSorted = Arrays.copyOf(lengthOfName, lengthOfName.length);
Arrays.sort(lengthOfNameSorted);

If you really need a sorted copy of an array.

I would generally prefer using

 IntStream.of(lengthOfName).sorted().boxed().collect(Collectors.toList());

unless the list is too big so that the memory and performance overhead of boxing is not negligible.

alex440
  • 1,789
  • 2
  • 16
  • 33
  • Thanks. I am really just trying to find the max of a given array. I was using the sort method so cause I know I can just refer to the last item of the sorted array to find the max. I looked for a max method online but didn't find any with method and clear description of how to use it. Do you know of any such method? – jay Mar 24 '18 at 02:24
  • @jay You don't need sort, which is O(nlogn), to find the max, which is O(n). You can use Arrays.stream(lengthOfName).max().getAsInt() – alex440 Mar 24 '18 at 02:45
1

As alex440 and markspace have already mentioned, the Java method Arrays.sort() is of type void, so it does not return a value as you'll find in the oracle spec for your specific case of Arrays.sort(int[]).

That being said, if you can't think of any compelling reason to have both of the arrays in the code you provided, you might just remove the lengthOfNameSorted array all together since you're already sorting lengthOfName.

ryanm
  • 441
  • 2
  • 7
  • thanks. I do have a compelling reason, as I will be comparing the two arrays. I am really just trying to find the max of a given array. I was using the sort method so cause I know I can just refer to the last item of the sorted array to find the max. I looked for a max method online but didn't find any with method and clear description of how to use it. Do you know of any such method? – jay Mar 24 '18 at 02:35
  • As far as I can remember, in Java there is no `max()` method for Arrays. There _is_, however, a `Collections.max()` method for Collection types. [This answer](https://stackoverflow.com/a/2607335/9542960) is pretty straight forward and should get you pointed in the right direction. A quick search for Java Collections `.max()` method should fill in any gaps. If you're only going to be working with arrays of 3 elements, then the Collections route may be overkill as there are simpler ways to find the maximum of 3 items in just a few lines of code. – ryanm Mar 24 '18 at 03:04
  • thanks. ya, i know i can create a loop to essentially duplicate a max function, but I am learning java and I am trying to get my self familiar with the built-in functions of java. – jay Mar 24 '18 at 17:58