reversing an arraylist readintarraylist
play

Reversing an ArrayList readIntArrayList() /* Reads the data into - PDF document

Student Responsibilities Mat 2170 Week 14 Reading: Textbook, Chapter 11 ArrayList Lab: ArrayList, input files, bar chart Attendance Spring 2008 1 2 The ArrayList Class ArrayList Methods The java.util package includes a class


  1. Student Responsibilities Mat 2170 Week 14 ◮ Reading: Textbook, Chapter 11 ArrayList ◮ Lab: ArrayList, input files, bar chart ◮ Attendance Spring 2008 1 2 The ArrayList Class ArrayList Methods ◮ The java.util package includes a class called boolean add(<T> element) ArrayList that extends the usefulness of arrays by Adds a new element to the end of the ArrayList ; providing additional operations. the return value is always true void add(int index, <T> element) ◮ Since ArrayList is a class, all operations on ArrayList Inserts a new element into the ArrayList ; objects are indicated using method calls. before the position specified by index ◮ The most obvious differences from simple Java arrays: <T> remove(int index) ◮ A new ArrayList object is created by calling the Removes the element at the specified position and ArrayList constructor returns that value ◮ The number of elements is obtained by invoking the size() boolean remove(<T> element) method, rather than selecting the length field. Removes the first instance of element , if it appears; ◮ get() and set() methods are used to select individual returns true if a match is found elements. void clear() ◮ In the summary of ArrayList methods which follows, the Removes all elements from the ArrayList notation < T > indicates the base type. 3 4 Generic Types in Java int size() Returns the number of elements in the ArrayList ◮ The < T > notation used in the previous slides is a placeholder for the element type used in the array. <T> get(int index) Returns the object at the specified index ◮ Class definitions that include a type parameter are called <T> set(int index, <T> value) Sets the element at the specified index to the new generic types. value and returns the old value ◮ When we declare or create an ArrayList , it is a good idea indexOf(<T> value) Returns the index of the first occurrence of the to specify the element type in angle brackets . For example: specified value, or − 1 if it does not appear ArrayList<String> names = boolean contains(<T> value) Returns true if the ArrayList contains the new ArrayList<String>(); specified value ◮ Doing this allows Java to check for the correct element boolean isEmpty() Returns true if the ArrayList contains no elements type when set() is called, and eliminates the need for a type cast when get() is called. 5 6

  2. Wrapper Classes Using Wrapper Classes ◮ Java designers chose to separate primitive types from the ◮ We can create an instance of a wrapper class by calling its standard class hierarchy, mostly for efficiency reasons. constructor with the primitive value, e.g., to create a new Integer object: ◮ Primitive Java values take less space and allow Java to use more of the capabilities provided by hardware. Integer maxItems = new Integer(5); ◮ The value stored in the object maxItems is a real object, ◮ However, there are times when the fact that primitive and we can use it in any contexts that require objects. types aren’t objects poses problems (e.g., there are tools in the Java libraries that work only with objects). ◮ This is particularly useful when we want to be able to change the value of a parameter that is passed into a ◮ To get around this problem, Java includes a wrapper class to method, since that is not allowed for primitive types. correspond to each of the primitive types: ◮ For each wrapper class, Java defines a method to retrieve boolean Boolean float ↔ Float ↔ the primitive value, e.g.: byte Byte int ↔ Integer ↔ char Character long ↔ Long ↔ int underlyingValue = maxItems.intValue(); double Double short ↔ Short ↔ 7 8 Boxing and Unboxing Generic Types and Boxing/Unboxing ◮ As stated, Java SE 5.0 automatically converts values back ◮ Java SE 5.0 automatically converts values back and forth and forth between a primitive type and the corresponding between a primitive type and the corresponding wrapper wrapper class. class. ◮ This allows an ArrayList object to store primitive values, ◮ For example, even though the elements of any ArrayList must be a Java class. Integer maxItems = 5; causes Java to call the Integer constructor. ◮ For example: ◮ Similarly, Java will automatically call intValue() before ArrayList <Integer> list = new ArrayList<Integer>(); the addition in this statement: list.add(42); int answer = list.get(0); int nextMax = maxItems + 1; In the second statement, Java uses boxing to enclose 42 in ◮ These operations are called boxing and unboxing . a wrapper object of type Integer ; the third statement unboxes the Integer to obtain the int . 9 10 Reversing an ArrayList readIntArrayList() /* Reads the data into the list */ import acm.program.*; import java.util.*; private ArrayList<Integer> readIntArrayList() { public class ReverseArrayList extends ConsoleProgram { ArrayList<Integer> list = new ArrayList<Integer>(); public void run() { int value = readInt(" ? "); println("This program reverses the elements " + while (value != SENTINEL) "in an ArrayList."); { println("Use " + SENTINEL + " to signal the " + list.add(value); "end of the list."); value = readInt(" ? "); } ArrayList<Integer> list = readIntArrayList(); reverseArrayList(list); return list; printIntArrayList(list); } } 11 12

  3. ArrayList Searching Methods * Reverses the data in an ArrayList */ private void reverseArrayList(ArrayList<Integer> list) { Method Description for (int i = 0; i < list.size() / 2; i++) returns true if the given value appears in { contains(value) swapElements(list, i, list.size() - i - 1); the list } Ex: list.contains("hello") } returns the index of the first occurrence indexOf(value) of the given value in the list ( − 1 if not /* Exchanges two elements in an ArrayList */ found) private void swapElements(ArrayList<Integer> list, Ex: list.indexOf("world") int p1, int p2) { returns the index of the last occurrence lastIndexOf(value) int temp = list.get(p1); of the given value in the list ( − 1 if not list.set(p1, list.get(p2)); found) list.set(p2, temp); } Ex: list.lastIndexOf("hello") Where list is an ArrayList<string> . /* Private constants --- Define the end-of-data value */ private static final int SENTINEL = 0; } // end of program 13 14 ArrayList Sorting and Binary Search Methods The java.util package contains a class called Collections which contains several useful static methods. Of particular interest: Method Description rearranges the elements into sort(list) sorted (non–decreasing) order Ex: Collections.sort(L) searches a sorted list for a given binarySearch(list, value) element value and returns its index Ex: Collections.binarySearch(L, "hello") When an ArrayList is sorted, binary search is much faster than linear search. 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend