• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Class objects into ArrayList

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to insert class objects into ArrayList,HashSet either of there,

please explain too....

Thanks
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
myListOrSet.add(myObject);
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks and Sorry,

none of the above suggestion provided the output.

infact they are giving compile time error
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic