Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing the ArrayList

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first step is to override the toString method of type A.
M
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic