java - adding multiple data to Object array at run time -
how set values 2 dimension array of objects in java.
following loop :
object[][] hexgensecurityinferencedata = null; (string methodname: knowgoodmap.keyset()) { hexgensecurityinferencedata = new object[][] { { (knowngoodinforo) knowgoodmap.get(methodname), new object[] { (methodpropertiesro) methodpropertiesmap.get(methodname), (list) methodparametersmap.get(methodname) } }, }; }
this prints 1 row of data. sure make mistake when adding values array of object don't know how fix.
kindly me fix this
you can't add elements array - can set elements in array.
i suggest have list<object[]>
instead:
list<object[]> hexgensecurityinferencedata = new arraylist<object[]>(); (string methodname:knowgoodmap.keyset()) { hexgensecurityinferencedata.add(new object[] { knowgoodmap.get(methodname), new object[] { methodpropertiesmap.get(methodname), methodparametersmap.get(methodname) } }); }
(i've removed casts pointless... you're storing values in object[]
anyway. benefit of casts cause exception if objects of unexpected type.)
you could still use array if wanted, you'd need create right size start with, , keep "current index". it's harder use arrays lists anyway.
if need array, can create 1 list:
object[][] array = hexgensecurityinferencedata.toarray(new object[0][]);
doing in 2 stages way simpler directly populating array up-front.
i'd suggest 2 further changes:
- don't use
object[]
this... create type encapsulate data. current approach, you've got nestedobject[]
withinobject[]
... code reading data horrible. - use
entryset()
instead ofkeyset()
, don't need fetch value key
Comments
Post a Comment