Java Methods ObjectOriented Programming and Data Structures 3
Java Methods Object-Oriented Programming and Data Structures 3 rd AP edition Maria Litvin ● Gary Litvin capacity size Chapter 11 java. util. Array. List Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 1
Objectives: • Learn about the java. util. Array. List class, its constructors and methods • Review some of the Array. List pitfalls • Practice with using Array. List in client classes and in subclasses 2
java. util. Array. List<E> • Implements a list using an array • Implements java. util. List<E> interface (discussed in Chapter 12) 3
java. util. Array. List<E> cont’d • Can only hold objects (of a specified type), not elements of primitive data types. • 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" . . . 4
Array. List Generics • Starting with Java 5, Array. List and other collection classes hold objects of a specified data type. • The elements’ data type is shown in angle brackets and becomes part of the List and Array. List type. For example: Array. List<String> words = new Array. List<String>(); List<Integer> nums = new Array. List<Integer>(); 5
Array. List<E> Constructors Java docs use the letter E as the type parameter for elements in generic collections Array. List<E> ( ) Creates an empty Array. List<E> of default capacity (ten) Array. List<E> (int capacity) Creates an empty Array. List<E> of the specified capacity 6
Array. List<E> Methods (a Subset) int size() boolean is. Empty () boolean add (E obj) returns true void add (int i, E obj) inserts obj as the i -th value; i must be from 0 to size() E set(int i, E obj) E get(int i) E remove(int i) boolean contains(E obj) int index. Of(E obj) i must be from 0 to size() -1 both use E’s equals to compare objects 7
Array. List Example Array. List<String> names = new Array. List<String>( ); names. add("Ben"); names. add("Cat"); names. add(0, "Amy"); System. out. println(names); Output [Amy, Ben, Cat] Array. List’s to. String method returns a string of all the elements, separated by commas, within [ ]. 8
Array. List<E> Details • 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() (or i > size() in add (i, obj) ) 9
Array. List<E> Autoboxing • If you need to put ints or doubles into a list, use a standard Java array or convert them into Integer or Double objects • Since Java 5, conversion from int to Integer and from double to Double is, in most cases, automatic (a feature known as autoboxing or autowrapping); the reverse conversion (called autounboxing) is also automatic. 10
Array. List<E> Autoboxing Example Array. List<Integer> counts = new Array. List<Integer>( ); counts. add(17); . . . int count = counts. get(0); Autoboxing: compiled as counts. add(new Integer(17)); Autounboxing: count gets the value 17 11
Lab: Shuffler • The Shuffler program shuffles the lines of text, then allows the user to restore the right order. 12
Lab: Shuffler (cont’d) • Your job is to write the Line. List class, with one field, Array. List<String>, and the following methods: Ø Ø Ø int size () — returns the number of lines String get (int k) — returns the line at index k void add (String line) — appends line to list String remove (int k) — removes and returns the k-th line void move(int index, int new. Index) — moves the line at index to the position at new. Index void shuffle() — shuffles the lines of text 13
Lab: Shuffler (cont’d) • Use the following algorithm for shuffle: 1. Set n to the size of the list; 2. Randomly select an element among the first n and swap it with the n-th element; 3. Decrement n by one; 4. Repeat Steps 2 -3 while n 2. 14
Array. List Pitfalls // Remove all occurrences // of "like" from words: int i = 0; Caution: when you remove elements, a simple forward for loop doesn’t work: while (i < words. size()) for (int i = 0; i < words. size(); i++) { { if ("like". equals(words. get(i)) words. remove(i); else } i++; } Shifts all the elements after the i-th to the left and decrements the size 15
“For Each” Loop • Works with List / Array. List: Array. List<String> words = new Array. List<String> ( ); . . . for (String word : words) {. . . // process word } Basically the same as: . . . for (int i = 0; i < words. size (); i++) { String word = words. get (i); . . . // process word } 16
Lab: Index Maker 17
Index Maker (cont’d) extends has Your job 18
Review: • When is an Array. List more convenient than an array? • Explain the difference between the capacity and size in an Array. List? • What method returns the number of elements currently stored in an Array. List? • What method is used to insert an element into an Array. List? 19
Review (cont’d): • Can a double value be stored in an Array. List<Double>? • What is autoboxing? • Can a “for each” loop be used with Array. Lists? • Can a class extend Array. List<String>? • Can an object change after it has been added to an Array. List? 20
- Slides: 20