Download Some examples of using an ArrayList

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSCI1402 Week20 Lecture2
Some examples of using an ArrayList
1. A list of Student objects (assume Student class exists)
import java.util.*;
public class StudentList
{
public static void main(String args[])
{
System.out.println(" Create an ArrayList named “students”");
ArrayList students = new ArrayList();
System.out.println(" Add some students");
students.add (new Student("Safia",18));
students.add(new Student("Malik",21));
System.out.println(students);
System.out.println(" Display the name of the students in position 0");
Student aStudent = (Student)students.get(0);
System.out.println(aStudent.getName()
System.out.println();
System.out.println(" Display the names of all of the students ");
for(int i = 0; i < students.size();i++)
{
Student aStudent = (Student) students.get(i);
System.out.println(aStudent.getName());
}
System.out.println(" Add a new student to position 0");
students.add(0,new Student("Sue",21));
System.out.println(" Display the names of all of the students again ");
for(int i = 0; i < students.size();i++)
{
Student aStudent = (Student) students.get(i);
System.out.println(aStudent.getName());
}
System.out.println(" Remove the student from position 1");
students.remove(1);
System.out.println(" Display the names of all of the students again ");
Page 1 of 4
CSCI1402 Week20 Lecture2
System.out.println(" Find out if the list is empty ");
if(students.isEmpty())
System.out.println("The list is empty");
else
System.out.println("The list is NOT empty");
System.out.println(" Add some more students");
students.add(0,new Student("Anne",21));
students.add(0,new Student("Heena",21));
System.out.println();
System.out.println(" Display the name of the students in position 0");
for(int i = 0; i < students.size();i++)
{
Student aStudent = (Student) students.get(i);
if(aStudent.getAge() == 21)
{
System.out.println(aStudent.toString());
}
} // end for loop
} // end main method
} // end class
Page 2 of 4
CSCI1402 Week20 Lecture2
1. A list of String objects
import java.util.*;
public class NameList
{
public static void main(String args[])
{
ArrayList names = new ArrayList();
names.add("Anne");
names.add("Heena");
names.add("Jamie");
names.add("John");
names.add("Yusuf");
names.add("Judith");
System.out.println(" Display all the names in the list ");
System.out.println();
for(int i = 0; i < names.size();i++)
{
String aName = (String) names.get(i);
System.out.println("The name is position " + i + " is " + aName);
}
System.out.println(" Display all the names that begin with ‘J’ ");
System.out.println();
for(int i = 0; i < names.size();i++)
{
String aName = (String) names.get(i);
if(aName.charAt(0)=='J')
{
System.out.println("The name is position " + i + " is " + aName);
}
}
System.out.println(" Using an Iterator to display all the names in the list ");
Iterator iter = names.iterator();
while(iter.hasNext())
{
String aName = (String) (iter.next());
System.out.println(aName);
}
} // end main
} // end class
Page 3 of 4
CSCI1402 Week20 Lecture2
1. A list of Integer objects - for you to comment
import java.util.*;
public class NumberList
{
public static void main(String args[])
{
ArrayList numbers = new ArrayList();
/*ArrayLists cannot be used to store primitive type data such as int. Java
provides “wrapper” classes to allow us to convert primitive types into
objects.One of these wrapper classes is the Integer class */
numbers.add(new Integer(1));
numbers.add(new Integer(11));
numbers.add(new Integer(7));
numbers.add(new Integer(44));
numbers.add(new Integer(3));
numbers.add(new Integer(4));
numbers.add(new Integer(101));
for(int i = 0; i < numbers.size();i++)
{
Integer aNumber = (Integer) numbers.get(i);
System.out.println("The number in position " + i + " is " + aNumber);
}
for(int i = 0; i < numbers.size();i++)
{
Integer aNumber = (Integer) numbers.get(i);
if(aNumber.intValue() < 10) // get the int value of the Integer object
{
System.out.println("The number in position " + i + " is " + aNumber);
}
}
Iterator iter = numbers.iterator();
while(iter.hasNext())
{
Integer aNumber = (Integer) (iter.next());
System.out.println(aNumber);
}
} // end main
} // end class
Page 4 of 4