aspose file tools*
The moose likes Beginning Java and the fly likes Arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arraylist" Watch "Arraylist" New topic
Author

Arraylist

Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
Hello,
I just want to know how to use arraylist in Java.
What I mean is, if I have 2 class(Book, and main class).
How to use the arraylist to construct new book?
Thank you
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

so far what you have done?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

A list of what? ArrayLists contains lists of some object or other. In what way do you want to create a book from a list of other objects?
Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
Thanks for reply.


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

OK, I still don't quite understand what you're asking. What you're doing there is creating a bunch of Book objects and adding them to an ArrayList - that's not the same as using an ArrayList to create a Book.

There's clearly something missing from the second class listing (what's the class called? BookList?), and it's a bit strange that you've got a static ArrayList, which you then populate in what looks like it's supposed to be a constructor. So what is it you're trying to achieve?
Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
oky, I want to list all the books.
when I do
System.out.println(bookList);
Output:
@1513

So, how I want to make sure that the array is added in the arrayList?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

Your booklist is an object. when you do System.out.println(booklist), you are calling the toString method of the ArrayList - which by default prints out (more or less) the memory address of the object.

What you probably want to do is call the toString() method on each object IN the array. You'll need to write a loop that iterates across all of them and prints out each book.

It's easy enough to write a method that does this, and you just pass it an ArrayList, so that it will work for ANY ArrayList.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
oky thanks.
I will try it.
Thanks =)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

And remember: when you print out each Book, you either need to specifically print out the informatiohn you want (e.g. book.getTitle()), or you need to implement toString() so that when you do System.out.println(book) it does what you want.
Anne Saizan
Ranch Hand

Joined: May 07, 2011
Posts: 35
thank you so much guys.=)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Arraylist