0

So I need to create a method which would take ints and return an array of length 3 with those three ints as they have been input.

This is what I have created, but it returns this [I@7852e922

public class IntsToArray {
  public static int[] fill(int a, int b, int c){
      int[] array = new int[3];
      array[0] = a;
      array[1] = b;
      array[2] = c;
      return (array);
  }
    public static void main(String[] args){
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);
        int[] array = fill(a, b, c);
        System.out.println(array);
    }
}

What am I doing wrong?

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
A.Bg
  • 65
  • 3
  • 11
  • "so my problem is I am not allowed to use anything from the library. So simple solutions only" I think @Juan s answer is about as simple as it gets, you could also use a range based loop `for(int ele : array){ System.out.println(ele); }` – George Dec 04 '17 at 21:44

3 Answers3

1

When you pass the array to System.out.println() the array's toString() method is called and the resulting string is written. In the case of an array there is no specific implementation of toString() so the toString() of the super class Object is used. Object.toString() is defined as:

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

So that is the [I@7852e922 that you are seeing. Essentially this is an internal id for the instance of your array.

To print the contents of the array do this:

System.out.println(Arrays.toString(array));

or this:

for (int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
}
bhspencer
  • 13,086
  • 5
  • 35
  • 44
1

If you want to print an array you should iterate over all of it's members and print each one. What you are doing at this moment is trying to print the array object which prints it's memory address uses the default toString() method of the object base class, as pointed out by bhspencer, to print the array.

Try this:

public class IntsToArray {
  public static int[] fill(int a, int b, int c){
      int[] array = new int[3];
      array[0] = a;
      array[1] = b;
      array[2] = c;
      return (array);
  }
    public static void main(String[] args){
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);
        int[] array = fill(a, b, c);

        for(int i = 0; i < array.length; i++)
        {
            System.out.println(array[i]);
        }
    }
}
Immac
  • 466
  • 1
  • 5
  • 16
  • 1
    awesome, thank you very much! The exact answer I needed. – A.Bg Dec 04 '17 at 21:49
  • 2
    You say that "prints it's memory address". This is not correct. In fact you are printing the String representation of the hash of the array plus the class name. See my answer for details, – bhspencer Dec 05 '17 at 16:18
0

As you've mentioned about not to use library, you can extract the code from class Arrays:

public static String stringify(int[] a) {
    if (a == null)
        return "null";
    int iMax = a.length - 1;
    if (iMax == -1)
        return "[]";

    StringBuilder b = new StringBuilder();
    b.append('[');
    for (int i = 0; ; i++) {
        b.append(a[i]);
        if (i == iMax)
            return b.append(']').toString();
        b.append(", ");
    }
}

And use it as follow:

public class IntsToArray {
    public static int[] fill(int a, int b, int c){
      int[] array = new int[3];
      array[0] = a;
      array[1] = b;
      array[2] = c;
      return (array);
    }
    public static void main(String[] args){
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);
        int[] array = fill(a, b, c);

        System.out.println(stringify(array));
    }
}
Ele
  • 33,468
  • 7
  • 37
  • 75