Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arraylist

 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so far what you have done?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.


 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oky thanks.
I will try it.
Thanks =)
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much guys.=)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic