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?
    }
}
share|improve this question

67% accept rate
3  
How do you convert from String to Custom? – Alex Sep 20 at 12:05
Can you tell me if I can do some parsing in my Custom class? – devel Sep 20 at 12:06
1  
refresh your question, and put clear question – Samir Mangroliya Sep 20 at 12:06
what about your custom class and your string data ? – user1645941 Sep 20 at 12:06
1  
Show us your Custom class. – Alex Sep 20 at 12:07
show 2 more comments
feedback

3 Answers

up vote 4 down vote accepted

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);
   }
}
share|improve this answer
feedback

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);
share|improve this answer
feedback

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)

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.