Java Methods ObjectOriented Programming and Data Structures 2
Java Methods Object-Oriented Programming and Data Structures 2 nd AP edition with Grid. World Maria Litvin ● Gary Litvin capacity size Chapter 13 java. util. Array. List Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 1
Objectives: • Learn about java. util. List interface • Learn about the java. util. Array. List class, its constructors and methods • Review some of the Array. List pitfalls • Practice with Part 4 of Grid. World ― “Critters” 2
java. util. Array. List<E> • Implements a list using an array • Implements java. util. List<E> interface 3
java. util. Array. List<E> cont’d • Implements a list using an array. • 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 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 • In 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
Array. List Pitfalls // Remove all occurences // of "like" from words: int i = 0; Caution: when you remove elements, a simple 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 12
“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 } 13
Lab: Index Maker 14
Index Maker (cont’d) extends has Your job 15
Grid. World’s Critters • Described in Part 4 of the Grid. World case study • Critter is subclass of Actor • You will create subclasses of Critter • Study the examples provided with Grid. World: Chameleon. Critter and Crab. Critter 16
Grid. World’s Critters (cont’d) public void act() { if (get. Grid() == null) return; Array. List<Actor> actors = get. Actors( ); process. Actors(actors); Array. List<Location> move. Locs = get. Move. Locations( ); Location loc = select. Move. Location(move. Locs); make. Move(loc); } • Do not override the act method in Critter’s subclasses; override other methods to achieve the desired functionality 17
Grid. World’s Critters (cont’d) • Pay attention to postconditions when you override Critter’s methods! Array. List<Actor> get. Actors( ) The state of all actors in the grid remains unchanged. void process. Actors(Array. List<Actor> actors) The state of this actor can change (except its location). The states of actors in the actors list can change. Some of the actors from the list can be removed. New actors can be added in empty grid locations. All other actors in the grid remain unchanged. 18
Grid. World’s Critters (cont’d) Array. List<Location> get. Move. Locations( ) The state of all actors in the grid remains unchanged. Location select. Move. Location (Array. List<Location> move. Locs) The state of all actors in the grid remains unchanged. void make. Move(Location loc) If loc is null, this critter is removed from the grid; otherwise this critter moves to loc. This critter’s state can change. A new actor can be added in this critter’s old location. The state of all other actors in the grid remains unchanged. 19
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? 20
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? 21
Review (cont’d): • Name three Critter’s methods that cannot change the state of any actor in the grid. • Which Critter’s methods are allowed to add new actors to the grid? • Which Critter’s methods are allowed to change the location of this critter? 22
- Slides: 22