



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




with Java, for Beginners
CIS110 2
CIS110 3
Person[] people; people = new Person[3]; people[0]=new Person(“jo”); people[1]=new Person(“flo”); CIS110 4
CIS110 5
Add an element Get an element Remove an element Search for an element Learn the size of the list Perform an operation on all elements
How to create a type-specific List (for Points,Strings) A much more convenient List, the one provided in the API, that works on any type CIS110 6
CIS110 7 A Type-Specific List (for Points) public boolean add(Point p) Add p to the list. Returns false if duplicates are not permitted and already contains the specified element public boolean remove(Point p) Remove the first occurrence of p.Returns true if this collection contained the specified element public boolean contains(Point p) Test whether the list contains p public int size() Number of elements in list public Point get(int i) Get the ith element of the list (starts from 0) 0 <= i && i < size() public void set(int i, Point p) Set the ith element to p 0 <= i && i < size() CIS110 8 Using a type-specific List
PointList points = new PointList(); points.add(new Point(1.0,2.0)); points.add(new Point(3.0,4.0)); points.add(new Point(5.0,6.0)); points.size()
3
points.get(2).getX()
CIS110 13
Example: ArrayList with Strings
import java.util.*; List words = new ArrayList(); words.add("cat"); words.add("rat"); words.get(1) "rat" for (int i = 0;i < words.size(); i++) System.out.println(words.get(i)); One approach for processing all items in a List: CIS110 14
An array of type BaseType[] can only hold objects of type BaseType An attempt to store an object of the wrong type in the array will be detected by the compiler There is no need to type-cast items that are retrieved from the array back to type BaseType. Instead of using the plain "List" type, it is possible to use List
CIS110 17