3

I've been trying and seeking for questions which are similar to mine for almost 2 days hope I will find out the answer, but no, I couldn't so I decided to ask you guys here.

This method is to print out all the keys and values of a HashMap<String, int[]> ratingmap. So keys are Strings and values are arrays. I've been working on that and below is my code.

public void showRatingsMap() {
    for (String customer: ratingmap.keySet()) {
        String key = customer.toString();
        int[] value = ratingmap.get(key);
        System.out.println("Customer: " + key + " - Rating: " + value);
    }
}

I'm really confused at the moment because the result which is printed out looks like this:

Customer: Douglas Anderson - Rating: [I@4f5b571e
Customer: Sidney - Rating: [I@75b49b45
Customer: Apollo - Rating: [I@243e0b62
Customer: Leslie - Rating: [I@655d6184

As I expect the rating to be an array, but it always appears as the weird combination above: [I@2b9fd66a

Could anyone please point out the mistakes that cause the problem?

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
user1382202
  • 31
  • 1
  • 2
  • possible duplicate of [Simplest way to print an array in Java](http://stackoverflow.com/questions/409784/simplest-way-to-print-an-array-in-java) – assylias May 08 '12 at 13:49
  • Those "weird combinations" are object references. What type of objects are stored in ratingmap? – DrewCo May 08 '12 at 13:50

4 Answers4

6

Arrays don't override the default toString() method. In order to print a representation of their contents, you can use Arrays.toString(int[]).

See my answer here for more details.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • Sorry but I don't really get it, could you please be more specific. Cause I've just started Java. I've tried (for my code): value.toString(int[] value), but it doesn't work :) – user1382202 May 08 '12 at 14:53
  • 1
    @user1382202 use `Arrays.toString(value)` instead of just `value` in the code of your question. – assylias May 08 '12 at 14:58
  • Oh, I've figured out, because I didn't import java.util.Arrays; :) Thanks :) – user1382202 May 08 '12 at 15:04
2

Value is "int[]", an array of ints. But when you convert that object to string, it doesn't automatically print all the elements of the array for you. It only prints the position in memory of the array, which is what you see.

You need to iterate over the elements of the array in order to print them. Like this:

for (int v : value) {
    System.out.println(v);
}

Or, as Paul said (Use a Helper class, like Arrays)

Luciano
  • 8,552
  • 5
  • 32
  • 56
1

What you see is the result of built-in toString on integer array types -- it prints "type of array" [I part and "reference" part, e.g., @75b49b45, which differs on per-instance basis inside the same JVM.

What you need is to manually iterate over array items and print each separately:

    int array[] = new int[] {3, 1, 4, 1};
    for (int i : array)
        System.out.print(i + " ");
    System.out.println();

Or you may convert array to List which has more friendly toString() implementation:

    int array[] = new int[] {3, 1, 4, 1};
    List<Integer> asList = new ArrayList<Integer>();

    for (int i : array)
        asList.add(i);
    System.out.println(asList);

Regarding starting point, there's explanation why you see [I -- it's because arrays have built-in toString which is inherited from toString() implementation of Object class and the latter works by printing type code of class it's called upon, then @ then internal id of instance of class. And type code for integer arrays is [I.

See full description of possible of type codes in javadoc of Class#getName() in JDK.

Victor Sorokin
  • 11,878
  • 2
  • 35
  • 51
1

The simplest solution is to use HashMap<String, List<Integer>> as this will print as you might expect.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130