Results 1 to 4 of 4
  1. #1
    FALL3N1 is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Accessing Elements of a Reflected ArrayList

    hey there
    I have a reflected a class to get a list of it's vars and vals... I've done this with If-statements looping through each var. I can get vals for the other kinds of vars:
    Java Code:
    if (type.equals("class java.io.File"))  
        {  
        try {  
         Object tween = field.get(o);  
         File castFile = (File)(tween);  
         val = castFile.getAbsolutePath();  
         type = "File";  
            
        } catch (Exception ee) {}  
           
        } else if (type.equals("class [Ljava.lang.String;")) {  
          try {  
          Object object = field.get(o);  
          String[] backToStringArray = (String[])(object);  
             
          val = "";  
          for (int i = 0; i < backToStringArray.length; i++)  
          {  
             val += backToStringArray[i] + ", ";  
          }  
             
          val = val.substring(0, val.length() - 2);  
             
           val = "{" + val + "}";  
           type = "String[]";  
                  
          } catch (Exception ee) {}  
           
        } else if (type.equals("class java.lang.String")) {  
            try {  
               Object obj = field.get(o);  
               val = obj.toString();  
               type = "String";  
            
        } catch (Exception ee) {}  
           
         } else if (type.equals("class java.util.ArrayList")) {  
           val = "foo"; // HERE IS WHERE I NEED TO FILL IN  
           type = "ArrayList<?>";  
       
           
         } else {  
          try {  
            Object other = field.get(o);  
            val = type;  
      
          } catch (Exception ee) {}  
      
             
        }
    The solution to this problem with a String[] was casting it and creating a new String[]... but that didn't work for an ArrayList, maybe because I can't know what kind of Objects are stored in the ArrayList... but I tried casting them as ArrayList<String> and ArrayList<File> but in both cases, no matter which kind of Object the ArrayList really held, neither way worked.. How do I get the elements stored in the ArrayList?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Accessing Elements of a Reflected ArrayList

    What do you mean by "neither way worked"?
    (And eating exceptions like that is generally unwise).
    Please do not ask for code as refusal often offends.

  3. #3
    FALL3N1 is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Accessing Elements of a Reflected ArrayList

    Oh right, sorry, how silly of me to forget... I think I can get to the end though
    Last edited by FALL3N1; 03-21-2012 at 11:49 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Accessing Elements of a Reflected ArrayList

    So this is sorted then?
    Please do not ask for code as refusal often offends.

Similar Threads

  1. How to selection the elements in an ArrayList?
    By MarMer in forum New To Java
    Replies: 11
    Last Post: 06-09-2011, 01:28 AM
  2. Adding elements to an ArrayList
    By ArcherSam in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2011, 03:05 PM
  3. Cannot swap elements in ArrayList
    By glchau in forum New To Java
    Replies: 4
    Last Post: 05-30-2010, 08:03 AM
  4. Replies: 5
    Last Post: 01-18-2010, 07:00 AM
  5. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM

Tags for this Thread

Posting Permissions

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