Results 1 to 4 of 4
- 03-20-2012, 09:03 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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:
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?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) {} }
- 03-21-2012, 09:46 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 03-21-2012, 11:15 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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.
- 03-22-2012, 09:49 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
How to selection the elements in an ArrayList?
By MarMer in forum New To JavaReplies: 11Last Post: 06-09-2011, 01:28 AM -
Adding elements to an ArrayList
By ArcherSam in forum Advanced JavaReplies: 7Last Post: 01-28-2011, 03:05 PM -
Cannot swap elements in ArrayList
By glchau in forum New To JavaReplies: 4Last Post: 05-30-2010, 08:03 AM -
Accessing elements of an ArrayList which contains 2D arrays
By Willi in forum New To JavaReplies: 5Last Post: 01-18-2010, 07:00 AM -
Help printing specific ArrayList elements
By CirKuT in forum New To JavaReplies: 5Last Post: 02-03-2009, 12:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks