© A+ Computer Science - www.apluscompsci.com ArrayList © A+ Computer Science - www.apluscompsci.com Lab 16.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
Static Class Members Wrapper Classes Autoboxing Unboxing.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
(c) University of Washington14-1 CSC 143 Java Collections.
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
© A+ Computer Science - Which of the following statements assigns the letter S to the third row and first column of a two-dimensional.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
© A+ Computer Science - Variables Data Types © A+ Computer Science - Lab 0B.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Collections Dwight Deugo Nesa Matic
The ArrayList Data Structure Standard Arrays at High Speed!
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
1 M255 Object-oriented programming with Java Prepared By: Prof. Dr. Shehab Gamalel-Din Unit 10 Collections: Sets & Maps.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Elevens is a lab about classes and Lists. List is a major concept being tested by the Elevens lab. Elevens.
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
Software Development Java Collections
University of Central Florida COP 3330 Object Oriented Programming
Java Collections Overview
Multiple Choice -answer the easiest question 1st
ArrayLists.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Methods Copying Autoboxing
ArrayLists 22-Feb-19.
Collections Framework
Review: libraries and packages
A+ Computer Science AP Review 2019 AP CS A EXAM
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com ArrayList © A+ Computer Science - www.apluscompsci.com Lab 16

© A+ Computer Science - www.apluscompsci.com ArrayList Arraylist is a class that houses an array. It is internally implemented in Java as an array. An ArrayList can store any type. It can store a combination of types. All ArrayLists store the first reference at index/position/subscript 0. ArrayList can store a reference to any type of Object. ArrayList was built using an array[] of object references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is an array? int[] nums = new int[10]; //Java int array 0 1 2 3 4 5 6 7 8 9 nums An array is a group of items all of the same type which are accessed through a single identifier. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList ArrayList is a descendant of List (which extends Collection), but because List and Collection are interfaces, you cannot instantiate them. Collection bad = new Collection(); //illegal List ray = new ArrayList(); //legal ArrayList list = new ArrayList(); //legal In the example above, ray is an ArrayList that stores Object references. In order to call non-Object methods on a spot in ray, casting would be required. ray.add(0,"hello"); out.println(((String)ray.get(0)).charAt(0)); ray and list store Object references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList References ArrayList list; list null null nothing A reference variable is used to store the location of an Object. In most situations, a reference stores the actual memory address of an Object. list stores the location / memory address of an ArrayList. list is a reference to an ArrayList. © A+ Computer Science - www.apluscompsci.com

ArrayList Instantiation © A+ Computer Science - www.apluscompsci.com new ArrayList(); 0x213 [] ArrayLists are Objects. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList ArrayList list = new ArrayList(); list 0x213 0x213 [] A reference variable is used to store the location of an Object. In most situations, a reference stores the actual memory address of an Object. list stores the location / memory address of an ArrayList. list is a reference to an ArrayList. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList Example of an ArrayList without the type <T> identifier: List ray = new ArrayList(); ray.add("hello"); ray.add(“school"); ray.add("contests"); System.out.println(( (String) ray.get(0)).substring(0,1)); System.out.println(( (String) ray.get(2)). substring(0,1)); OUTPUT h c In the example above, ray is an ArrayList that stores Object references. In order to call non-Object methods on a spot in ray, casting would be required. ray.add(0,"hello"); out.println(((String)ray.get(0)).charAt(0)); casting (must cast in order to send messages to the object) ray stores Object references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open arraylistobjects.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Generic ArrayLists Generic ArrayLists are array lists that take a type parameter. In other words you identify the type of all the elements in the array list. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList With Java 5, you can now specify which type of reference you want to store in the ArrayList. ArrayList<String> words; words = new ArrayList<String>(); ArrayList<Double> decNums; decNums = new ArrayList<Double>(); In the example above, words can only store String references. decNums can only store Double references. Java knows the exact type of reference in both ArrayLists; thus, there is no need for casting when accessing class specific methods. words.add("Hello"); out.println(words.get(0).charAt(0)); © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList With Java 5, you can now specify which type of reference you want to store in the ArrayList. ArrayList<Integer> bigStuff; bigStuff = new ArrayList<Integer>(); ArrayList<Student> studentList; studentList = new ArrayList<Student>(); In the example above, words can only store String references. decNums can only store Double references. Java knows the exact type of reference in both ArrayLists; thus, there is no need for casting when accessing class specific methods. itList.add(new It(34.21)); out.println(itList.get(0).getIt()); © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList List<String> ray; ray = new ArrayList<String>(); ray.add("hello"); ray.add(“school"); ray.add("contests"); System.out.println(ray.get(0).substring(0,1)); System.out.println(ray.get(2).substring(0,1)); OUTPUT h c In the example above, ray is an ArrayList that stores String references. Casting would not be required to call non-Object methods on ray. ray.add(0,"hello"); ray.add(1,"chicken"); out.println(ray.get(0).charAt(0)); out.println(ray.get(1).charAt(5)); ray stores String references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open arraylistgenerics.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList Methods © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com ArrayList frequently used methods Name Use add(item) adds item to the end of the list add(index,item) adds item at index – shifts items up-> set(index,item) replaces item at index (returns old element) (similar to z[index]=item) get(index) returns the item at index (similar to z[index] ) size() returns the # of items in the list (similar to z.length) remove(index) removes the item at index (returns element that was removed) remove(value) removes first item that matches this value (returns true if item was found, else false) clear() removes all items from the list import java.util.ArrayList; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com add() ArrayList<String> words; words = new ArrayList<String>(); words.add(0,"it"); words.add("is"); words.add("a"); words.add("lie"); System.out.println(words); OUTPUT [it, is, a, lie] The add(item) method adds item to the end of the ArrayList. The add(spot, item) method adds the item at the spot specified. All other items are shifted toward the end of the ArrayList. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com add() ArrayList<Integer> nums; nums = new ArrayList<Integer>(); nums.add(34); nums.add(0,99); nums.add(21); nums.add(11); System.out.println(nums); OUTPUT [99, 34, 21, 11] The add(item) method adds item to the end of the ArrayList. The add(spot, item) method adds the item at the spot specified. All other items are shifted toward the end of the ArrayList. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open arraylistaddone.java arraylistaddtwo.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com set() ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.set(0,66); ray.add(53); ray.set(1,93); ray.add(22); System.out.println(ray); OUTPUT [66, 93, 53, 22] The add(item) method adds item to the end of the ArrayList. The set(spot, item) method replaces the reference at spot with item. The location / address of item is placed in spot. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com size() for (int i=0; i<ray.size(); i++) { System.out.println(ray.get(i)); } The size() method returns the number of items in the ArrayList. If the ArrayList is storing seven references, size() would return a 7. .size( ) returns the number of elements (logical/physical size) in the array list. © A+ Computer Science - www.apluscompsci.com

get() OUTPUT23.23 65.6 ArrayList<Double> ray; ray = new ArrayList<Double>(); ray.add(23.23); ray.add(11.11); ray.add(12.1); ray.add(65.6); System.out.println(ray.get(0)); System.out.println(ray.get(3)); OUTPUT23.23 65.6 The get(spot) method returns the reference stored at spot. .get(index) returns the reference stored at the index! © A+ Computer Science - www.apluscompsci.com

get() OUTPUT23 11 12 65 ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(12); ray.add(65); for(int i=0; i<ray.size(); i++) System.out.println(ray.get(i)); OUTPUT23 11 12 65 The get(spot) method returns the reference stored at spot. .get(index) returns the reference stored at the index! © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com enhanced for loop ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(12); ray.add(65); for( int val : ray ) System.out.println(val); OUTPUT23 11 12 65 The ehanced for loop pulls one value at a time from ray and puts each value in val. val receives one value from ray each iteration. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open arraylistset.java arraylistget.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com remove methods © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com remove() ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("b"); ray.remove(0); ray.add("c"); ray.add("d"); System.out.println(ray); OUTPUT [c, d] The remove method will remove the item at the specified spot / location or the specified value. When an item is removed, all items above the removed item are shifted down toward the front of the ArrayList. All items are shifted to the left. [a, b] becomes [b] [b, c, d] becomes [c, d] © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com remove() ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("b"); ray.remove("a"); ray.add("c"); ray.add("d"); ray.remove("d"); System.out.println(ray); OUTPUT [b, c] The remove method will remove the item at the specified spot / location or the specified value. When an item is removed, all items above the removed item are shifted down toward the front of the ArrayList. All items are shifted to the left. [a, b] becomes [b] [b, c, d] becomes [b, c] © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open arraylistremoveone.java arraylistremovetwo.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com clear() ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("x"); ray.clear(); ray.add("t"); ray.add("w"); System.out.println(ray); OUTPUT [t, w] The clear() method removes all items from the ArrayList. The ArrayList becomes an [] empty ArrayList. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open arraylistclear.java © A+ Computer Science - www.apluscompsci.com

What, no initializer list? How can you initialize an ArrayList? ArrayList<String> names = new ArrayList<String>(); names.add(“Smith”); names.add(“Jones”); Or similar to an initializer list for an array: ArrayList<String> names = new ArrayList<String> (Arrays.asList("Smith", "Jones")); import java.util.ArrayList; import java.util.Arrays; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com EmployeeArrayList Purpose: To get familiar with ArrayLists Make a copy of your EmployeeNames and EmployeeNamesTester and name them EmployeeArrayList and EmployeeArrayListTester. Change all occurrences of arrays to ArrayLists. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com enhanced for loop ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(53); for(int num : ray) { System.out.println(num); } OUTPUT 23 11 53 The new for loop is great to print out Arrays and Collections. The new for loop extracts an item from ray each time it iterates. The new for loop is an iterator based loop. Once the loop reaches the end of ray, it stops iterating. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open newforloopone.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AutoBoxing AutoUnboxing © A+ Computer Science - www.apluscompsci.com

Objects (wrapper class) © A+ Computer Science - www.apluscompsci.com Box/Unbox Primitive types Objects (wrapper class) byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean == .equals() For each of the primitive types, there is a corresponding wrapper class. int has a corresponding wrapper class named Integer. int stores a non-decimal value. Integer stores the location / memory address of an Integer Object which stores an int value. double has a corresponding wrapper class named Double. double stores decimal and non-decimal values. Double stores the location / memory address of a Double Object which stores a double value. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Box/Unbox Before Java 5 added in autoboxing and autounboxing, you had to manually wrap primitives. Integer x = new Integer(98); or int y = 56; x= new Integer(y); Before autoboxing and autounboxing, a reference could only refer to a primitive if a Wrapper class was instantiated and the primitive passed to the constructor. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Box/Unbox Java now wraps automatically. Integer num1 = 99; and Integer num1 = new Integer(99); These two lines are equivalent. Integer numOne = 99; is equivalent to Integer numOne = new Integer(99); With the introduction of Java 5, the new Integer() Object instantiation code happens in the background. Java takes care of these details, but does not show the work. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Box/Unbox Java now wraps automatically. Double num1 = 99.1; and Double num1 = new Double(99.1); These two lines are equivalent. Double numOne = 99; is equivalent to Double numOne = new Double(99); With the introduction of Java 5, the new Double() Object instantiation code happens in the background. Java takes care of these details, but does not show the work. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Box/Unbox Before Java 5 added in autoboxing and autounboxing, you had to manually unwrap references. If you boxed 98 as an Integer like this: Integer ref = new Integer(98); Then you had to unbox it like this: int y = ref.intValue(); Before autoboxing and autounboxing, a reference value could only be stored in a primitive if the corresponding reference get method was called to retrieve the primitive value from the reference. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Box/Unbox Java now unwraps automatically. int prim; Integer num = new Integer(3); OLD WAY: prim=num.intValue(); System.out.println(prim); NEW WAY: prim = num; These two lines are equivalent. OUTPUT 3 3 Integer numOne = new Integer(99); int primInt = numOne; is equivalent to int primInt = numOne.intValue(); With the introduction of Java 5, the intValue() method call happens in the background. Java takes care of these details, but does not show the work. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Box/Unbox OUTPUT 9.3 0 -1 1 Double dub = 9.3; double prim = dub; System.out.println(prim); int num = 12; Integer big = num; System.out.println(big.compareTo(12)); System.out.println(big.compareTo(17)); System.out.println(big.compareTo(10)); Before autoboxing and autounboxing, a reference could only refer to a primitive if a Wrapper class was instantiated and the primitive passed to the constructor. Before autoboxing and autounboxing, a reference value could only be stored in a primitive if the corresponding reference get method was called to retrieve the primitive value from the reference. With the introduction of Java 5, the wrapping and unwrapping / boxing and unboxing happens in the background. Java takes care of these details, but does not show the work. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open autoboxunbox.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com enhanced for loop ArrayList<Integer> ray; ray = new ArrayList<Integer>(); //add some values to ray int total = 0; for(Integer num : ray) { //this line shows the AP preferred way //it shows the manual retrieval of the primitive total = total + num.intValue(); // total += num.intValue(); //the line below accomplishes the same as the line above //but it uses autounboxing to get the primitive value total = total + num; // total += num; } System.out.println(total); OUTPUT153 The new for loop is great to print out Arrays and Collections. The new for loop extracts an item from ray each time it iterates. The new for loop is an iterator based loop. Once the loop reaches the end of ray, it stops iterating. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open newforloopone.java newforlooptwo.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Start work on Lab 16a (Factor List) © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList with User-defined classes © A+ Computer Science - www.apluscompsci.com

ArrayList w/User Classes public class Creature implements Comparable { private int size; public Creature(int girth) size=girth; } public void setSize(int girth) public String toString() return “” + size; //Other methods not shown here Instance variable Constructor initializes size of Creature Creature is a class used to store information about Creatures. toString() returns the String representation of Creature © A+ Computer Science - www.apluscompsci.com

ArrayList w/User Classes © A+ Computer Science - www.apluscompsci.com ArrayList<Creature> creatureList; creatureList = new ArrayList<Creature>(); creatureList.add(new Creature(4)); creatureList.add(new Creature(9)); creatureList.add(new Creature(1)); 0 1 2 0x12 0x32 0xD2 creatureList Creature 4 Creature 9 Creature 1 © A+ Computer Science - www.apluscompsci.com

ArrayList w/User Classes © A+ Computer Science - www.apluscompsci.com ArrayList<Creature> creatureList; creatureList = new ArrayList<Creature>(); creatureList.add(new Creature(4)); creatureList.add(new Creature(9)); creatureList.add(new Creature(1)); System.out.println(creatureList.get(0)); creatureList.get(0).setSize(7); System.out.println(creatureList.get(2)); OUTPUT 4 7 1 Creatures is an ArrayList that stores reference to Creature objects. In this example, creatures stores the addresses / locations of 3 Creature objects. © A+ Computer Science - www.apluscompsci.com

ArrayList w/User Classes © A+ Computer Science - www.apluscompsci.com creatureList.get(0) . setSize(7); 0x242 0x242 Creature What does this return? What does the . dot do? Creatures.get(0) returns the address / location of a Creature object. Applying a . dot to a reference grants you access to the object referred to by the reference. .setSize() is a Creature method that changes the size property of the Creature. The . dot grants access to the Object at the stored address. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open ArrayListUserClassesOne.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open Creatures.java CreaturesRunner.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Collections class Ends with an s © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com Collections frequently used methods Name Use sort(x) Puts all items in array list x in ascending order binarySearch(x,y) Prerequisite: array list x must be sorted. Checks x for the location of y. This method returns the index where y is found. If y is not found, it returns -1 minus (the position in the array list where x would be added). fill(x,y) Fills all elements in x with value y rotate(x,y) Shifts items in x left or right y locations reverse(x) Reverses the order of the items in x import java.util.Collections; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Collections ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(66); ray.add(53); Collections.sort(ray); System.out.println(ray); System.out.println(Collections.binarySearch(ray,677)); System.out.println(Collections.binarySearch(ray,66)); OUTPUT [11, 23, 53, 66] -5 3 Collections.sort() will put all items in natural ascending order. Collectoins.binarySearch() will locate an item. If the item does not exist, binarySearch() will return -1+ -(where the value would be if it was there). -3 is -1 + -2(2 is the spot where the item would be) © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Collections ArrayList<Integer> ray = ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(53); ray.add(100); System.out.println(ray); Collections.rotate(ray,2); Collections.rotate(ray,2); // back to [23, 11, 53, 100] Collections.reverse(ray); OUTPUT [23, 11, 53, 100] [53, 100, 23, 11] [100, 53, 11, 23] Collections.rotate() rotates items to the right or to the left a specified number of spots / positions. A negative number rotates to the left and a positive number rotates to the right. Collections. reverse() reverses the order of all items. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Collections ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(0); ray.add(20); ray.add(50); System.out.println(ray); Collections.fill(ray,33); OUTPUT [0, 20, 50] [33, 33, 33] Collections.fill() will fill in all spots with a specified value. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open BinarySearch.java Rotate.java fill.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com search methods © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com ArrayList frequently used methods Name Use contains(y) Checks to see if the list contains y. Returns either true or false. indexOf(y) Checks the array list for the location of y. Returns the index where y is found. If not found, it returns -1. isEmpty() Returns true if the array list is empty (no elements) or false if it contains elements. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com search methods ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(66); ray.add(53); System.out.println(ray); System.out.println(ray.indexOf(21)); System.out.println(ray.indexOf(66)); System.out.println(ray.contains(21)); System.out.println(ray.contains(66)); OUTPUT [23, 11, 66, 53] -1 2 false true © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open Search.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Open ArrayListUserClassesTwo.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Collection and List Interfaces © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Java Interfaces The following are important interfaces included in the Java language: Collection List © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Map Java Collection Sub Interfaces -extends Collection SortedMap Implementing Classes Sub Interfaces -extends HashMap HashTable Implementing Classes List Set Sub Interfaces -extends TreeMap Implementing Classes SortedSet This Collections hierarchy chart is very important. It is a must to know which classes implement which interfaces and which interfaces extend which interfaces. Implementing Classes ArrayList LinkedList Vector Stack TreeSet AbstractSet HashSet LinkedHashSet © A+ Computer Science - www.apluscompsci.com

The Collection Interface © A+ Computer Science - www.apluscompsci.com The Collection interface is the parent of List and Set. The Collection interface has many methods including add(), clear(), remove(), and size(). Collection List Set others not shown © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com The List Interface The List interface extends the Collection interface. Additionally, the List interface also has the get() method as well as several other methods. List ArrayList LinkedList others not shown © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList ArrayList is a descendant of List and Collection, but because List and Collection are interfaces, you cannot instantiate them. Collection bad = new Collection(); //illegal List justAsBad = new List(); //illegal List list = new ArrayList(); //legal ArrayList aList = new ArrayList(); //legal Collection col = new ArrayList(); //legal ** ** Legal only if using methods from Collection interface In the example above, ray is an ArrayList that stores Object references. In order to call non-Object methods on a spot in ray, casting would be required. ray.add(0,"hello"); out.println(((String)ray.get(0)).charAt(0)); list, aList, and col store Object references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Continue work on Word and WordList classes © A+ Computer Science - www.apluscompsci.com