Converting an array of strings into and object?
I have a list of strings.
String result[] = { "1=AccountId93",
"10188=930.0", "10190=Mkt930", "1=AccountId94",
"10188=940.0", "10190=Mkt940", "1=AccountId95",
"10188=开发者_如何学运维950.0", "10190=Mkt950" };
I want to put list into an array of objects where by the object is e.g AccountId93,930.0,Mkt930
How can I reorder this array??? N.b there is name value pairs to work with but a map will not work as the key values will overwrite each other
List<Item> items = new ArrayList<Item>();
for (int i = 0; i < result.length; i+=3) {
String value1 = result[i ].split("=")[1];
String value2 = result[i+1].split("=")[1];
String value3 = result[i+2].split("=")[1];
items.add(new Item(value1, value2, value3));
}
This will do it. Item
is your class that represents a data set stored in the array of strings.
精彩评论