0

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

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

2
  • you can refer this discussion also... see this Commented Jan 24, 2011 at 8:19
  • 5
    Never ever name an own class Object, even though it is legal Java code. Commented Jan 24, 2011 at 8:19

2 Answers 2

1
List<Object> lstObj = new ArrayList<Object>();
for(String str: strList){
  if(str==null)
     lstObj.add(null);
  }else{
     lstObj.add((Object)(str));
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I think he means a custom Object. not java.lang.Object
@The, ArrayList<Object> is mentioned explicitly in the OP.
@peter i didn't believe it either, however it does say in the question Object is a class written by me..
@The Scrum Meister - I think org.life.java noticed that (and quickly invented the custom constructor :-) )
@org.life.java - Jigar Joshi - no, no, revert your last change! The copy constructor was the best answer at all!!! As long as this Object is really a custom class or a "placeholder" name for any other class.
|
0

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 ) );
}

Comments

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.