Kĩ thuật lập trình - Chapter13: Java. util. ArrayList

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.

ppt22 trang | Chia sẻ: huyhoang44 | Lượt xem: 529 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Kĩ thuật lập trình - Chapter13: Java. util. ArrayList, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
java.util.ArrayListCopyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.Java MethodsObject-Oriented Programmingand Data StructuresMaria Litvin ● Gary Litvin2nd AP edition  with GridWorldChapter13 sizecapacity1Objectives:Learn about java.util.List interfaceLearn about the java.util.ArrayList class, its constructors and methodsReview some of the ArrayList pitfallsPractice with Part 4 of GridWorld ― “Critters”2java.util.ArrayListImplements a list using an arrayImplements java.util.List interface3java.util.ArrayList cont’dImplements 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)"Cat""Hat""Bat"capacitysize...4ArrayList  GenericsStarting with Java 5, ArrayList 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 ArrayList type. For example: ArrayList words = new ArrayList(); List nums = new ArrayList();5ArrayList ConstructorsArrayList ( )ArrayList (int capacity)Creates an empty ArrayList of default capacity (ten)Java docs use the letter E as the type parameter for elements in generic collectionsCreates an empty ArrayList of the specified capacity6ArrayList Methods (a Subset)int size()boolean isEmpty ()boolean add (E obj)void add (int i, E obj)E set(int i, E obj)E get(int i)E remove(int i)boolean contains(E obj)int indexOf(E obj)both use equals to compare objectsi must be from 0 to size() -1inserts obj as the i-th value; i must be from 0 to size()returns true7ArrayList Example ArrayList names = new ArrayList( ); names.add("Ben"); names.add("Cat"); names.add(0, "Amy"); System.out.println(names);[Amy, Ben, Cat]OutputArrayList’s toString method returns a string of all the elements, separated by commas, within [ ].8ArrayList DetailsAutomatically 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 IndexOutOfBoundsException when i size() in add (i, obj) )9ArrayList AutoboxingIf you need to put ints or doubles into a list, use a standard Java array or convert them into Integer or Double objectsIn 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.10ArrayList Autoboxing Example ArrayList counts = new ArrayList( ); counts.add(17); ... int count = counts.get(0);Autoboxing: compiled ascounts.add(new Integer(17));Autounboxing: count gets the value 1711ArrayList Pitfalls // Remove all occurences // of "like" from words: int i = 0; while (i words = new ArrayList ( );...for (String word : words){ ... // process word}...Basically the same as:for (int i = 0; i actors = getActors( ); processActors(actors); ArrayList moveLocs = getMoveLocations( ); Location loc = selectMoveLocation(moveLocs); makeMove(loc); }Do not override the act method in Critter’s subclasses; override other methods to achieve the desired functionality17GridWorld’s Critters (cont’d)ArrayList getActors( )Pay attention to postconditions when you override Critter’s methods!The state of all actors in the grid remains unchanged.void processActors(ArrayList 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.18GridWorld’s Critters (cont’d)Location selectMoveLocation (ArrayList moveLocs)void makeMove(Location loc)ArrayList getMoveLocations( )The state of all actors in the grid remains unchanged.The state of all actors in the grid remains unchanged.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.19Review:When is an ArrayList more convenient than an array?Explain the difference between the capacity and size in an ArrayList?What method returns the number of elements currently stored in an ArrayList?What method is used to insert an element into an ArrayList?20Review (cont’d):Can a double value be stored in an ArrayList?What is autoboxing?Can a “for each” loop be used with ArrayLists?Can a class extend ArrayList?Can an object change after it has been added to an ArrayList?21Review (cont’d):Name the 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

Các file đính kèm theo tài liệu này:

  • pptch13_1174.ppt