• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert String to a Custom Object  RSS feed

 
Alen Grace
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I woud like to convert a string(which is in an array) to a custom object.
Also, if I have different custom objects, will it be ok if I use ?


 
Devaka Cooray
Marshal
Posts: 5649
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a "custom object"? Why do you need to convert a String value to another form of object, given that String itself is an object? Do you probably need to add more functionality to the value? If that's the case, creating a class that wraps the String might be what you are looking for.

And yes, you can put several objects of YourClass to an ArrayList, but I'd suggest you change the generic type of your list to something that makes sense:

ArrayList<YourClass> o;
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the OP means creating an object out of data which is available as a String (http://www.coderanch.com/t/642425/java/java/Convert-String-Custom-Object)
So, some data like 'Maneesh M 43' would be mapped to a Person object with variables for name, gender, age

@Alen, If that is the case, then like Devaka suggested, you should have a List<Person>people (instead of <Object>)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alen Grace wrote:I woud like to convert a string(which is in an array) to a custom object.
Also, if I have different custom objects, will it be ok if I use ?

Simple answer: No.

You can certainly convert a String to another type - in fact, it's probably not a bad idea. But using a List to hold several different types is probably not - UNLESS they all have a common superclass or implement the same interface; in which case you could use the superclass or interface type.

Using Object as a type, simply because it's the superclass to everything, is almost always WRONG. Indeed, it defeats the whole purpose of a statically-typed language like Java.

Think about it: What is a List<Object>?
It's a list of "somethings" - I don't know what they are right now, and they might all be different, but when I do find out, I'll be sure to let you know.

Would you want to write a procedure based on a spec as vague as that? You'll spend half your life working out what type each object is - which is precisely what static typing is designed to avoid.

HIH

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!