Results 1 to 2 of 2

Thread: Java Reflection:Extracing individual item from an array of unknown type/dimension

Hybrid View

  1. #1

    Default Java Reflection:Extracing individual item from an array of unknown type/dimension

    I am trying to use Java Reflection APIs for extracting all member items from an array of unknown type and unknown dimension??


    Any ideas?

  2. #2
    Join Date
    Jan 2010
    Posts
    3

    Default

    I would imagine doing something like (pseudocode)

    Code:
    dump(Object obj) {
      if ( obj == null ) {
        ...handle null...
      } else if ( obj.getClass().isArray() ) {
        for ( int i =0; i < Array.getLength(obj); i++ ) {
          dump(Array.get(obj,i));
        }
      } else {
        ...handle non-array object...
      }
    }
    Of course, in real life you may prefer non-recursive approach, put Array.getLength out of loop, etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •