0

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

3
  • 1
    It would help if you could explain what the different elements in the array are and what you want the target Account Object to look like Commented Apr 27, 2011 at 11:04
  • I thought I did sorry. Result is a list of strings. tag 1 = accountID tag 10188 = Price tag 10190 = marketPrice These three things make up the Account Object. So there currently just a list. but the first three strings will make up object instance 1. The second three will make up instance 2 and so on Commented Apr 27, 2011 at 11:10
  • A basic way to go about your problem with such a poorly constructed list, would be to create an object that has the three requisite properties; iterate through the list (assuming you need all the items), instantiating an object for each three items. Commented Apr 27, 2011 at 11:15

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes the tags are strictly ordered. Thanks folks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.