Summary that Pulls All the Concepts TOGETHER (Ref.
13)
The following code pulls all the concepts discussed
in the lat 5 references together so that you can have an
accurate picture of what is happening. Study this
carefully.
...the code involver a main application and a student class
...the java.util package is imported thereby giving you
access to the collection framework
...an ArrayList object is created and a reference is stored in an interface variable called list
...a lot of things happened behind the scenes when we created
this object . Refer to
Reference 11 above and you will see where the
ArrayList class exists in the
hierarchy of the collection framework. Remember the rules on
inheritance and you will
understand that the following happened when the object was
created:
...interface Collection {...}
...interface List extends Collection
{...}
...abstract class AbstractCollection
implements Collection {...}
...abstract
class AbstractList extends AbstractCollection implements List {...}
...class ArrayList extends
AbstractList {...}
...a reference to this ArrayList is stored in a
List interface variable called list
...I declared and ititialized a String array and an int array (each holding 10
values)
...a for loop new is used to create 10 separate Student objects
...the constructor for the Student
class receives an arbitrary ID (passed as the for loop
value +1)
...the getName and getAge methods are
called in the loop to pass a name and age to
each Student object
...Important-- the last statement in
the loop uses the add method on the list variable to place
a reference to the
specific Student object, just created, in the ArrayList. Notice how list
is referred to
(which is an interface variable of type List) . This is very IMPORTANT
because it shows
how methods available to the interface type can be applied and the
parameter can be
any object (in this case a reference to a stu object).
...Because we are now playing with an
interface type variable, many more possibilities are available
that are more
general in scope and more powerful and useful to programmers.
...the next for loop works backwards and does the following:
...uses the get method on the list
variable and stores the reference in a variable called s of type Object
...uses the toString method on that
object and because this method was also defined in the
Student class
then the original toString method (which was bound to the object) was the one
that was applied to
the s object. (Refer to Reference 10)
...the next command merely prints the
data id, name, age
...The above loop demonstrates casting, method binding, inheritance, and
polymorphism in 3 statements.
...A bland line is printed
...An Iterator variable called iter is created. Iterator is a multipurpose
interface that provided methods
for traversing some data structure. It is applied to the List
reference variable called list.
...A while loop is then executed as long as there are values to be printed.
Remember the hasNext method
of the Iterator interface is applied on the iter reference
variable, which held
a reference to the list reference
variable, which in turn held a reference to the ArrayList, which was the
concrete structure that held references
to the 10 Student objects.
...The toString method in the Student class allowed the data to be printed
...The final line hold the output on the screen
package untitled163;
import java.util.*;
import javax.swing.*;
public class Application1
{
public static void main(String args[])
{
List list = new ArrayList();
String[] names={"Mary","Paul","Susan","Maria","Cody","William","Travis","Ellen","Robert","Troy"};
int[] ages={22,21,34,45,32,20,19,28,29,17};
for(int a=0;a<=9;a++)
{
Student stu = new Student(a+1);
stu.getName(names[a]);
stu.getAge(ages[a]);
list.add(stu);
}
for (int i = 9; i >= 0; i--)
{
Object s=list.get(i);
String g=s.toString();
System.out.println(g);
}
System.out.println(" ");
Iterator iter = list.iterator();
while (iter.hasNext())
System.out.println(iter.next());
JOptionPane.showMessageDialog(null,"lll");
}
}
//**********************************************************
package untitled163;
public class Student
{
private int id;
private String name;
private int age;
public Student(int x)
{
id=x;
}
public void getName(String n)
{
name=n;
}
public void getAge(int y)
{
age=y;
}
public String toString()
{
String s=id+" "+name+" "+age;
return s;
}
}