2

How can I convert values here:

List<String> values = new ArrayList<String>

to :

ArrayList<Custom>

EDIT:

public class Custom {


    public Custom Parse(String input) {
        // What should I do here?
    }
}
6
  • 3
    How do you convert from String to Custom? Commented Sep 20, 2012 at 12:05
  • Can you tell me if I can do some parsing in my Custom class? Commented Sep 20, 2012 at 12:06
  • 1
    refresh your question, and put clear question Commented Sep 20, 2012 at 12:06
  • what about your custom class and your string data ? Commented Sep 20, 2012 at 12:06
  • custom class for parsing ...see here ... Commented Sep 20, 2012 at 12:08

3 Answers 3

4

You could use:

List<Custom> customList = new ArrayList<Custom>();
for (String value: values) {
   customList.add(new Custom(value));
}

Although it would be better just to add a constructor with a String argument:

class Custom {
   private final String input;

   public Custom(String input) {
      this.input = input;
   }

   // not needed but implemented for completeness    
   public static Custom parse(String input) {
      return new Custom(input);
   }
}
0

Assuming your list of Custom objects has the same size with values list. With one enhanced for-loop, set the appropriate fields of your objects like this:

int i=0;

for(String str:values)
   customList.get(i++).setSomeProperty(str);
0

you can find a solution using Google Collections libraries on this thread Converting a List<String> to a List<Integer> (or any class that extends Number)

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.