-5

I'm trying to figure out why my code isn't outputting the coin change permutations list as a list of int arrays. It's outputting some hex value (or whatever I@64578ceb is).

Any thoughts?

Call:

System.out.println("Permutations List: " + dan.makeChange(27));

Code:

public class Person {
int[] denominations, coinValues;
List<int[]> resultsList;

public Person() {
    denominations = new int[]{25, 10, 5, 1};
    resultsList = new ArrayList<int[]>();
}


public List<int[]> makeChange(int change) {
   return resultsList= changeMaker(change, new int[] {0,0,0,0});
}

public List<int[]> changeMaker(int change, int[] toAdd) {               
    if (change == 0) {
        resultsList.add(toAdd);
        return resultsList;
    }

    for (int i = 0; i < denominations.length; i++) {
        if (change >= denominations[i]) {
            int[] temp = toAdd;
            temp[i]++;
            resultsList = changeMaker(change-denominations[i], temp);
        }
    }
    return resultsList;
}

Output:

Permutations List: [[I@64578ceb, [I@64578ceb, [I@64578ceb, [I@64578ceb, [I@64578ceb, [I@64578ceb, etc...
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user3871
  • 12,432
  • 33
  • 128
  • 268

5 Answers5

4

int[] doesn't have pretty toString() for you to display numbers, You need to write your own method to accept int[] and print in whatever format you want

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    Maybe use `Arrays.toString`? Of course, this might/not fit the intentions of OP but its a valid way and a built in pretty printer. – zw324 May 13 '13 at 19:12
1

System.out.println method doesn't know how to print arrays in any type.

In case you want to display int array, build a function that takes an int[] and return string, and then wrap every entry at the List with that function when you printing it out.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

That's how arrays are printed in Java. You need to loop and "pretty-print" the arrays yourself with Arrays.toString():

List<String> printable = new ArrayList<> ();
for (int[] array : list) {
    printable.add(Arrays.toString(array));
}
System.out.println(printable);
assylias
  • 321,522
  • 82
  • 660
  • 783
0

[I@64578ceb is the printed representation of an int[]. Your permutations list is a list of int[], so you're seeing the elements of the list surrounded by [ and ], and the elements, each of which looks something like [I@64578ceb. For a very quick and dirty solution, you could use the List.toArray method and Arrays.deepToString to get a string representation of your list of arrays.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
0

To print the content of an array either iterate over each element in the array:

int[] array = new int[10];

for(int s : array) System.out.println(s);
// or
for(int i = 0; i < array.length; i++) System.out.println(array[i]);

or use Arrays.toString():

int[] array = new int[10];
System.out.println(Arrays.toString(array));

which will print something like:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

It's outputting some hex value (or whatever I@64578ceb is).

Let me explain the following simple example:

int[] array = new int[10];
System.out.println(array);

array is an object, hence you are calling println(Object) of PrintStream (System.out), which calls toString() on the passed object internally. The array's toString() is similar to Object's toString():

getClass().getName() + "@" + Integer.toHexString(hashCode());

So the output would be something like:

[I@756a7c99

where [ represnts the depth of the array, and I refers to int. 756a7c99 is the value returned from hashCode() as a hex number.

Read also Class.getName() JavaDoc.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417