Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Can someone guide me how to convert ArrayList<String> to ArrayList<Object> ??

Object is a class written by me with some getters and setters..

share|improve this question
you can refer this discussion also... see this – Vivek Jan 24 '11 at 8:19
5  
Never ever name an own class Object, even though it is legal Java code. – Andreas_D Jan 24 '11 at 8:19

2 Answers

List<Object> lstObj = new ArrayList<Object>();
for(String str: strList){
  if(str==null)
     lstObj.add(null);
  }else{
     lstObj.add((Object)(str));
  }
}
share|improve this answer
I think he means a custom Object. not java.lang.Object – The Scrum Meister Jan 24 '11 at 8:19
@The, ArrayList<Object> is mentioned explicitly in the OP. – Péter Török Jan 24 '11 at 8:21
@peter i didn't believe it either, however it does say in the question Object is a class written by me.. – The Scrum Meister Jan 24 '11 at 8:23
1  
@The Scrum Meister - I think org.life.java noticed that (and quickly invented the custom constructor :-) ) – Andreas_D Jan 24 '11 at 8:23
@The, oopsie :-( Nice example of confusing question... – Péter Török Jan 24 '11 at 8:25
show 3 more comments

Have a constructor in your object that takes a string:

List<MyObject> myObjectList = new ArrayList<MyObject>();
for ( String str : stringList )
{
  myObjectList.add( new MyObject( str ) );
}
share|improve this answer

Your Answer

 
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.