File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Printing the ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing the ArrayList" Watch "Printing the ArrayList" New topic
Author

Printing the ArrayList

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
I am adding an object of type A to an ArrayList.
where A a1 = new A("name of student");
A is a regular class.
List temp = new ArrayList();
temp.add(a1);
temp.add(a2);........
How do we print the ArrayList contents so that we get the names of all students in the ArrayList?
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Your first step is to override the toString method of type A.
M


Java Regular Expressions
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Thanks for the prompt reply. But how would I do it for the following code?
1. creating objects of-
class A a1: (new String("name of student 1"))
a2: (new String("name of Student 2"))
a3: (new String("name of student 3"))
class B b1 (new String("name of student 1"))
b2: (new String("name of Student 2"))

ArrayList li = [object of A, object of B, object of C... and so on]
Print li;
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Lucky, I'm have some problems understanding your question. What do you mean by

1. creating objects of-
class A a1: (new String("name of student 1"))
a2: (new String("name of Student 2"))
a3: (new String("name of student 3"))
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
I mean-
Section sec = new Section("School" );
sec.addval(new Student("Mike"));
sec.addval(new Student("Henry"));
sec.addval(new Student("John"));
Section sec2 = new Section("B" );
sec2.addval(new Student("Lacy"));
sec2.addval(new Student("Macy" ) );
sec.addval(sec2);
public class Section
{
ArrayList ar = new ArrayList(Object ...);
public void addval()
{
Add contents to ArrayList.
}
public void display()
{
Display contents of ArrayList such that it looks like this-
School
-Mike
-Henry
-John
-Section B
-Lacy
-Macy
}
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
So going back to my original advice, one answer is to override the toString method of the Student class and/or the Section class.
M
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One way is to get each thing in the list and print them one at a time. Look at the iterator() method on your list. Then look at the Iterator's hasNext and next methods. YOu can make a while loop to get one object at a time from the iterator and print it or add it to a String where you build up a message for printing. Is that where you wanted to go?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
This is generally the kind of HW question I assign when I want to demonstrate how polymorphism can work. I'm guessing that the assignment's point is showing how overriding the toString method will affect the output.
All best,
M
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Printing the ArrayList
 
Similar Threads
Generics
question about ArrayList
generics doubt
JTable
Can somebody help me understand this problem?