Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Arrays Gotchas and Wins Sorting

Arrays.sort giving weird output.

When using Arrays.sort, I'm getting the output "[Ljava.lang.String;@36baf30c", which doesn't seem to be an error, but I don't get the sorted array. I'm doing the work in VSCode rather than on the console. When I copied the code into the Workspace, I got the same thing: [Ljava.lang.String;@1b28cdfa (in searching this output, I read that the number is always different.)

Here's the code:

import java.util.Arrays; import java.util.Comparator;
import java.util.Random;

public class gotchasWins { //This is the filename, after the name of the //section. public static void main(String[] args) throws Exception { String[] friends = { "Treasure", "Ben", "Alena", "Pasan", "Craig" };

    Arrays.sort(friends, Comparator.comparing(String::length));

    System.out.println(friends.toString());
}

}

How do I resolve this to get the desired output of the sorted array?

1 Answer

Instead of System.out.println(friends.toString());, try System.out.println(friends);.