• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class objects into ArrayList  RSS feed

 
Amit kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to insert class objects into ArrayList,HashSet either of there,

please explain too....

Thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myListOrSet.add(myObject);
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be a language issue so I may not be understanding your question fully, but putting an object into an arraylist or a set is the same as anything else. If you are putting a string into an arraylist, you would simply use add. Same for a HashSet. For example:


If you want to use generics, then it is only slightly different:
[ August 16, 2007: Message edited by: Chad Clites ]
 
Amit kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Thanks for replying

what i wish to do is that i have created a seperate class .
--------------------------------------------------------------------------

-----------------------------------------------------------------------
i want to use the above to insert the values into the ArrayList
but not able to...............
-------------------------------------------------------------------------


Please guide to achieve this
as while running i am getting the hashcode.
[ August 16, 2007: Message edited by: Amit kumar ]
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList objArray = new ArrayList<String>();
objArray.add(new Person("Amit",20));


To make a code type safe you have to use the type parameter in both sides of the assignment. Since you want to add Person object into the ArrayList, the code should be



System.out.println(objArray);


In the println() method you are passing objArray of type ArrayList. Which actually calls the toString() method, returns the hashCode of the object. If you want a meaningful output for each object of the list, then you can achieve it as follows


[ August 16, 2007: Message edited by: Al Mamun ]
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't try but I think it works!

[ August 17, 2007: Message edited by: Manuel Leiria ]
 
Amit kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks and Sorry,

none of the above suggestion provided the output.

infact they are giving compile time error
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then tell us exactly what the error message is, that makes it much easier to find out what the problem is...
 
Amit kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am placing the output
this is am receiving from my code



but with suggestion
i am getting type mismatch error,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit kumar:
...Please guide to achieve this as while running i am getting the hashcode...

Is this what your question really is? How to get output for a Person that's not "com.practiceJava.Person@45a877"?

If that's the question, then the answer is to override the toString method in the Person class. This method is inherited from Object, and you are getting that default behavior. In your Person class, add...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!