1

Here is my code:

import java.util.Arrays;
import java.util.List;

public class ArrayDemo1 {
  public static void main (String args[]) {

    //reversing array and inspecting it using Arrays.deepToString()

    int[] array1 = new int[] {1,2,3,4};
    int[] reversed_array = ArrayDemo1.reverse(array1);
    for (int i=0; i < reversed_array.length; ++i) {
      System.out.println(reversed_array[i]);
    }
    System.out.println(Arrays.deepToString(reversed_array));
  }

  public static int[] reverse(int[] list) {
    int[] result = new int[list.length];

    for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
      result[j] = list[i];
    }
    return result;
  }
}

Error:

incompatible types: int[] cannot be converted to Object[]

It looks like in this post: What's the simplest way to print a Java array?

they use Arrays.deepToString to print out arrays of primitives right?

This is the code in that referenced post:

*// array of primitives:*
int[] intArray = new int[] {1, 2, 3, 4, 5};
*// for when you have other elements (other than strings) use the code below*
System.out.print(Arrays.deepToString(*your arrays name*));
*//output: [1, 2, 3, 4, 5]*
Community
  • 1
  • 1
Jwan622
  • 11,015
  • 21
  • 88
  • 181
  • 3
    Which specific answer are you talking about? For the most part, I'm seeing `deepToString` being used for arrays of arrays in that page... – Jon Skeet Jan 05 '16 at 18:07
  • if you change everything to Object[] where you have int[] it will work – JRowan Jan 05 '16 at 18:14
  • 1
    You can just use `Arrays.toString()` instead of `Arrays.deepToString()`. You have an array of `int`, not an array of arrays. – azurefrog Jan 05 '16 at 18:15
  • 3
    That answer has been incorrectly edited by someone also new to this subject and unfortunately edit was [approved (automatically)](http://stackoverflow.com/review/suggested-edits/10076556) because one of reviewers instead of rejecting it decided to improve it by correcting only one of problems with it, but didn't noticed other problems. I rolled back answer to its previous state which contains proper examples. – Pshemo Jan 05 '16 at 18:16
  • Great! Thanks a lot @Pshemo – Jwan622 Jan 05 '16 at 18:23

1 Answers1

4

The error is exactly what the compiler says. You have an int[], but you cannot pass it into a method that expects an Object[], much like you can't pass an int (or any other primitive) where an Object is required.

Using a recursive string conversion method such as Arrays.deepToString is unnecessary here. This method only takes an Object[], because only an array of object references can refer to other arrays, making the recursion necessary. An array of primitives can only hold primitive values -- there is no need to take each individual element and recursively call deepToString on it; they are primitive values already. In fact, Arrays.deepToString's javadocs state:

If an element e is an array of a primitive type, it is converted to a string as by invoking the appropriate overloading of Arrays.toString(e). If an element e is an array of a reference type, it is converted to a string as by invoking this method recursively.

The method Arrays.deepToString itself must notice if an internal element is a primitive array, and it calls Arrays.toString in that case.

Use Arrays.toString instead, which has overloads for each primitive array type.

rgettman
  • 176,041
  • 30
  • 275
  • 357