title: Pretty print array object, not array of objects?
body: <p>So, I've already discovered</p>
  
  <pre><code>Arrays.toString(arr);
  </code></pre>
  
  <p>So don't point me to <a href="https://stackoverflow.com/questions/409784/simplest-way-to-print-an-array-in-java">this question</a>.</p>
  
  <p>My problem is just a tiny bit different. In this case, I don't have a native array pointer to the array in question. I have it as an Object pointer, and it could be an array of any type (primitive or otherwise). In this case, I can use the above toString() method by casting the Object pointer to Object[]. However, if the pointer is a primitive array, it will throw a runtime exception and crash. So?</p>
  
  <p>Example:</p>
  
  <pre><code>double test[] = {1, 2, 3, 4};
  Object t = test;
  // Now how do I pretty print t as an array with no access to test?
  </code></pre>
  
  <p>I solved my problem with this:</p>
  
  <pre><code>public String unkObjectToString(Object o) {
      if(!o.getClass().isArray()) return o.toString();
      int len = Array.getLength(o);
      String ret = "[";
      for(int i = 0; i &lt; len; i++) {
          Object q = Array.get(o, i);
          ret += unkObjectToString(q);
          if(i == len - 1)
              ret += "]";
          else
              ret += ", ";
      }
      return ret;
  }
  </code></pre>