Docsity

Java Programming for Beginners: Understanding Lists and ArrayLists in CIS110, Study notes of Computer Science

Toggle sidebar
bg1
1
Introduction to Programming
with Java, for Beginners
ArrayList
CIS110 2
Collections
Data: Groups of entities of similar kind
CD collection
Items in store
Characters in a video game
Methods: Do the “same thing” to all of
them
Computing the total number of tracks
Adding sales tax to all the prices
Moving all of them one step forward
CIS110 3
Array Recap
Kind of a collection
Person[] people;
people = new Person[3];
people[0]=new Perso n(“jo”);
people[1]=new P erson(“flo” );
CIS110 4
Problems with Arrays
Fixed size
Optimal size may depend on information
unavailable at creation time
Not good with dynamic growth
Cannot add, and remove elements without
the pain of resizing
A collection that can dynamically resize is
known as a list
pf3
pf4
pf5

Partial preview of the text

Download Java Programming for Beginners: Understanding Lists and ArrayLists in CIS110 and more Study notes Computer Science in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners

ArrayList

CIS110 2

Collections

 Data: Groups of entities of similar kind

 CD collection

 Items in store

 Characters in a video game

 Methods: Do the “same thing” to all of

them

 Computing the total number of tracks

 Adding sales tax to all the prices

 Moving all of them one step forward

CIS110 3

Array Recap

 Kind of a collection

Person[] people; people = new Person[3]; people[0]=new Person(“jo”); people[1]=new Person(“flo”); CIS110 4

Problems with Arrays

 Fixed size

 Optimal size may depend on information

unavailable at creation time

 Not good with dynamic growth

 Cannot add, and remove elements without

the pain of resizing

 A collection that can dynamically resize is

known as a list

CIS110 5

Lists

 Basic requirements

 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

 We will look at:

 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

Visualizing a list

 A sequence of references to objects of a

given type

 Indexed by 0, 1, 2, ...

 A list of Points:

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

Typical Usage of List interface

 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

Some more problems

 Use of the type Object as the basic type

for objects that are stored in a list leads

 Need to use type-casting in almost every

case when an element is retrieved from that

list

 Since any type of object can legally be added

to the list, there is no way for the compiler to

detect an attempt to add the wrong type of

object to the list

  • The error will be detected only at run time i.e. when the object is retrieved from the list and the attempt to type-cast the object fails CIS110 15

Solution: Parameterized Types

 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  Where BaseType is any object type, that is, the name of a class or of an interface  BaseType cannot be one of the primitive types  Used to create lists that can hold only objects of type BaseType. For example,

  • List p1 = new ArrayList();
  • ArrayList p2 = new ArrayList(); CIS110 16 More on ArrayList Class Methods

 Javadocs for ArrayList show input or

return type as “E”

 “E” is nothing but what you declared you

to be

 Example: boolean add(E o)

 Adds element of type E to end of the list

 If E is not the type specified when you

created your ArrayList, i.e. you try to add

String instead of a Point then compiler will

complain.

  • This is why parameterized types were invented

CIS110 17

Sample Interactions

 Create an list to hold Points

 List list1 = new ArrayList();

 Add a Point to list

 list1.add(new Point(4,5));

 To learn the size of the list

 list1.size()

 Remove a Point at specified location

 list1.remove(0)

 To append one list to another:

list1.addAll(list2);