Java Object Oriented Programming Lists Objectives Learn about

Java Object Oriented Programming Lists

Objectives: • Learn about the Collections interface. • Learn about the List interface. • Learn about the Array. List class. Lab 06 - 2

Collection Interface • A Collection represents a group of objects known as its elements. The Collection interface is used to pass around collections of objects where maximum generality is desired. For example, by convention all general-purpose collection implementations have a constructor that takes a Collection argument. This constructor, known as a conversion constructor, initializes the new collection to contain all of the elements in the specified collection, whatever the given collection's subinterface or implementation type. In other words, it allows you to convert the collection's type. • The Collection interface contains methods that perform basic operations, such as int size(), boolean is. Empty(), boolean contains(Object element), boolean add(E element), boolean remove(Object element), and Iterator<E> iterator(). Lab 01 -3

public interface Collection<E> { public abstract boolean add(E e); public abstract void clear(); public abstract boolean contains(Object o); public abstract boolean is. Empty(); public Iterator<E> iterator(); public abstract boolean remove(Object o); public abstract int size(); public abstract Object[] to. Array(); } Lab 01 -4

List Interface A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements. In addition to the operations inherited from Collection, the List interface includes operations for the following: • Positional access — manipulates elements based on their numerical position in the list. This includes methods such as get, set, add, and remove. • Search — searches for a specified object in the list and returns its numerical position. Search methods include index. Of and last. Index. Of. • Iteration — extends Iterator semantics to take advantage of the list's sequential nature. The list. Iterator methods provide this behavior. • Range-view — The sublist method performs arbitrary range operations on the list. Lab 01 -5

public interface List<E> extends Collection<E> { public abstract void add(int index, E e); public abstract E get(int index); public abstract index. Of(Object o); public int last. Index. Of(Object o); public abstract E remove(int index); public abstract E set(int index, E element); public abstract List<E> sub. List(); } Lab 01 -6

Array. List The Array. List class implements the List interface. Array. List supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Lab 01 -7

java. util. Array. List • Implements a list as a resizable array. • Keeps track of the list capacity (the length of the allocated array) and list size (the number of elements currently in the list) capacity size "Cat" "Hat" "Bat" Lab 06 - 8

Array. List (cont’d) • Automatically increases (doubles) the capacity when the list runs out of space; allocates a bigger array and copies all the values into it • get(i) and set(i, obj) are efficient because an array provides random access to its elements • Throws Index. Out. Of. Bounds. Exception when • i < 0 or i size() Lab 06 - 9

Array. List (cont’d) • Array. List holds objects (of any type) • If you need to put ints or doubles into a list, use a standard Java array or convert them into Integer or Double objects • You have to remember what types of objects your put into the list and may need to cast a retrieved object back into its type Lab 06 - 10

Array. List (cont’d) l From Java API Docs: Lab 06 - 11

Array. List Methods (a Subset) int size(); boolean is. Empty(); boolean add(Object obj); returns true void add(int i, Object obj); inserts obj as the i -th value; i must be from 0 to size() Object set(int i, Object obj); Object get(int i); Object remove(int i); boolean contains(Object obj); int index. Of(Object obj); i must be from 0 to size() -1 use equals to compare objects Lab 06 - 12

Java Object Oriented Programming Begin Lab 06

Top-Down Programming (cont’d) Start JCreator. • Open the file “Lab 06. java”. • “Lab 06. java” is in your Lab 06Lab 06 folder. • Lab 06 - 14

Declare The Class/main method import java. util. *; import java. io. *; public class Lab 06 { public static void main(String[] args) { Lab 06 lab= new lab 06( ); lab. input( ); lab. output( ); } } Lab 06 - 15

Declare The Instance Fields private Array. List<String> list = new Array. List<String>(); Lab 06 - 16

Write The insert. In. Order Method public static void insert. In. Order(List<String> auto. List, String auto) { } Lab 06 - 17

Write The insert. In. Order Method public static void insert. In. Order(List<String> auto. List, String auto) { int index = 0; } Lab 06 - 18

Write The insert. In. Order Method public static void insert. In. Order(List<String> auto. List, String auto) { int index = 0; while (index < auto. List. size() && auto. compare. To(auto. List. get(index)) > 0) index++; } Lab 06 - 19

Write The insert. In. Order Method public static void insert. In. Order(List<String> auto. List, String auto) { int index = 0; while (index < auto. List. size() && auto. compare. To(auto. List. get(index)) > 0) index++; auto. List. add(index, auto); } Lab 06 - 20

Write The input( ) Method public void input() { } Lab 06 - 21

Write The input( ) Method public void input() { try { } catch (IOException e) { System. out. println(e); System. exit(0); } } Lab 06 - 22

Write The input( ) Method try { Scanner reader = new Scanner(new File("autos. dat")); } Lab 06 - 23

Write The input( ) Method try { Scanner reader = new Scanner(new File("autos. dat")); while (reader. has. Next. Line()) { } } Lab 06 - 24

Write The input( ) Method try { Scanner reader = new Scanner(new File("autos. dat")); while (reader. has. Next. Line()) { String auto = reader. next. Line(); } } Lab 06 - 25

Write The input( ) Method try { Scanner reader = new Scanner(new File("autos. dat")); while (reader. has. Next. Line()) { String auto = reader. next. Line(); insert. In. Order(auto); } } Lab 06 - 26

Write The input( ) Method catch (IOException e) { System. out. println(e); System. exit(0); } Lab 06 - 27

Write The output( ) Method public void output() { } Lab 06 - 28

Write The output( ) Method public void output() { for (int i = 0; i < list. size(); i++) System. out. println(list. get(i)); } Lab 06 - 29

Compile And Run Lab 06. java Lab 06 - 30

Programming Example l Open the file “Colors. java”. Colors. java is a short program that reads a series of colors from a data file and stores them in an Array. List of type Strng. We are going to write the method remove. All that receives a color (as a String) and removes all instances of that color from the Array. List. Lab 06 - 31

Contents Of “colors. dat” red red blue red green red Lab 06 - 32

Contents Of Colors. java import java. util. *; import java. io. *; public class Colors { private Array. List<String> list = new Array. List<String>(); public static void main (String[ ] args) { Colors colors = new Colors(); colors. input(); colors. output(); colors. remove. All("red"); colors. output(); } Lab 06 - 33

Contents Of Colors. java (cont…) public void input() { try { Scanner reader = new Scanner(new File("colors. dat")); while (reader. has. Next()) list. add(reader. next. Line()); } catch (IOException e) { System. out. println(e); System. exit(0); } } Lab 06 - 34

Contents Of Colors. java (cont…) public void output(){ for (int i = 0; i < list. size(); i++) System. out. println(list[i]); } Lab 06 - 35

Contents Of Colors. java (cont…) public void remove. All(String color) { System. out. println(); System. out. println("Removing all instances of " + color + " from the list!"); What steps are involved in removing all instances of color from list. System. out. println(); } list red red blue red green red 0 1 2 3 4 5 6 7 Lab 06 - 36

Write the remove. All Method public void remove. All(String color) { System. out. println(); System. out. println("Removing all instances of " + color + " from the list!"); for (int index = 0; index < list. size(); index++) { } System. out. println(); } list red red blue red green red 0 1 2 3 4 5 6 7 index Lab 06 - 37

Write the remove. All Method (cont…) public void remove. All(String color) { System. out. println(); System. out. println("Removing all instances of " + color + " from the list!"); for (int index = 0; index < list. size(); index++) If list was an array: { list[0]. equals(color) if (list. get(index). equals(color)) } System. out. println(); } list. get(index) returns “red”. red red blue red green red 0 1 2 3 4 5 6 7 index Lab 06 - 38

Write the remove. All Method (cont…) public void remove. All(String color) { System. out. println(); System. out. println("Removing all instances of " + color + " from the list!"); for (int index = 0; index < list. size(); index++) { if (list. get(index). equals(color)) list. remove(index); } System. out. println(); } list red red blue red green red 0 1 2 3 4 5 6 7 index Lab 06 - 39

Compile And Run Colors. java Did it work? Lab 06 - 40

Compile And Run Colors. java Lab 06 - 41

What Went Wrong? index 0 list. size() = 8 red red blue red green red 0 1 2 3 4 5 6 7 When list. remove(index) is called, the first instance of “red” is removed from the Array. List and all the other elements are shifted to the left, then the size variable of the list is decremented to 7. list. size() = 7 red red blue red green 0 1 2 3 4 5 After Calling list. remove(index) red 6 Lab 06 - 42

What Went Wrong (cont…)? index 1 list. size() = 7 red red blue red green red 0 1 2 3 4 5 6 • “red” at index position 1 is removed. • “red” at index position 0 is skipped. list. size() = 6 red blue red green 0 1 2 3 4 After Calling list. remove(index) red 5 Lab 06 - 43

How do we make remove. All work properly? Lab 06 - 44

Fixing remove. All public void remove. All(String color) { Decrement the System. out. println(); System. out. println("Removing all instances of " + color + " from the list!"); for (int index = 0; index < list. size(); index++) { if (list. get(index). equals(color)) { list. remove(index); index--; } } System. out. println(); } index Lab 06 - 45

Compile And Run Colors. java Lab 06 - 46

Working With ADT’s public class Person { private String first. Name; private String Last. Name; public Person(String first, String last) { first. Name = first; last. Name = last; } Accessors, Mutators, to. String(), etc… /* Other Methods Not Shown */ } Lab 06 - 47

Declaring An Array. List Of Type Person Array. List<Person> list = new Array. List<Person>(); or List<Person> list = new Array. List<Person>(); Lab 06 - 48

Adding Elements To An Array. List Of Type Person list. add(new Person(“Jim”, “Bowie”)); or String first. Name = “Jim”; String last. Name = “Bowie”; Person p = new Person(first. Name, last. Name); list. add(p); Lab 06 - 49

Processing An Array. List Of Type Person for (int i = 0; i < list. size(); i++) { Person p = list. get(i); /* do something with p */ } Lab 06 - 50

Java Object Oriented Programming Questions?

Java Object Oriented Programming Begin Lab 06
- Slides: 52