How to convert attributes list (like a map) to Object[]

时间:2016-07-11 21:39:58

标签: list object dictionary copy converter

I have an attrList, which contains a list of attributes with their name (key) and value. I want to copy all of them to Object[].

Available methods in attrList structure: getName(i), getValue(i) and size().

How can I convert/copy it?

Object[] result = new Object[attrList.size()];

for(int i=0; i < attrList.size(); i++) {
      result[i] = ?
}

Thanks in advance :)

1 个答案:

答案 0 :(得分:0)

If you are sure you want to put your result in a Object[] you can do it so:

Object[] result = new Object[attrList.size()];

for(int i=0; i < attrList.size(); i++) {
  result[i] = new Object[]{attrList.getName(i), attrList.getValue(i)};
}

Every element of your Object[] will be another array of Object made by two elements. But it will be hard to get keys and values from it.

If you want an appropriate Object to store your result, you can use a HashMap and put in each element the key and the value of your result:

HashMap<Object, Object> result = new HashMap<Object, Object>();

for(int i=0; i < attrList.size(); i++) {
    result.put(attrList.getName(i), attrList.getValue(i));
}