mat 2170
play

Mat 2170 Arrays ArrayLists Generic Types The ArrayList Class - PowerPoint PPT Presentation

Mat 2170 The ArrayList Class Week 13 Wrappers Mat 2170 Arrays ArrayLists Generic Types The ArrayList Class Patterns Examples Parameters Spring 2014 Lab 13 Using Lists Class Exercises Student Responsibilities Mat 2170 The


  1. Mat 2170 The ArrayList Class Week 13 Wrappers Mat 2170 Arrays ArrayLists Generic Types The ArrayList Class Patterns Examples Parameters Spring 2014 Lab 13 Using Lists Class Exercises

  2. Student Responsibilities Mat 2170 The ArrayList Class Week 13 Reminder: EXAM next Thursday, 4/24, at 7:00 pm Wrappers Arrays Picnic is Tuesday, 4/22 - get signed up. ArrayLists Generic Types Reading: Textbook, Chapter 11.8, The ArrayList class Patterns Examples Lab 13, utilizing the ArrayList class Parameters Lab 13 Attendance Using Lists Class Exercises

  3. Wrapper Classes Mat 2170 The ArrayList Java designers chose to separate primitive types from the Class standard class hierarchy, mostly for efficiency reasons. Week 13 Wrappers Arrays Primitive Java values take less space and allow Java to use ArrayLists more of the capabilities provided by hardware. Generic Types Patterns Examples Parameters However, there are times when the fact that primitive types Lab 13 aren’t objects poses problems Using Lists (e.g., there are tools in the Java libraries that work only with Class Exercises objects and not primitive types — one such is ArrayList ).

  4. Mat 2170 To get around this problem, Java includes a wrapper class to The ArrayList correspond to each of the following primitive types: Class ↔ ↔ boolean Boolean float Float Week 13 byte ↔ Byte int ↔ Integer Wrappers ↔ ↔ Arrays char Character long Long ArrayLists double ↔ Double short ↔ Short Generic Types Patterns All of the above primitive wrapper classes in Java are Examples immutable – their states (contents) cannot be modified Parameters after they are created, only replaced. Lab 13 Using Lists Class The value stored in an instance of any of the wrapper classes Exercises is an object , and we can use it in any context that requires an object.

  5. Boxing and Unboxing Mat 2170 Java SE 5.0 (and subsequent versions) automatically The ArrayList Class converts values back and forth between a primitive type and the corresponding wrapper class. These operations are called Week 13 boxing and unboxing . Wrappers Arrays ArrayLists For example, Integer maxItems = 5; Generic Types Patterns causes Java to call the Integer constructor, and is Examples Parameters equivalent to: Integer maxItems = new Integer(5); Lab 13 Using Lists Class Similarly, int nextMax = maxItems + 1; Exercises is equivalent to: int nextMax = maxItems.intValue()+1;

  6. Storing Large Amounts of Data Mat 2170 The ArrayList Class It is often the case that in order to solve a problem by Week 13 computer, we need to be able to store an unknown and / or Wrappers a large number of data items. Arrays ArrayLists Generic Types It would be difficult to create individual names and storage Patterns locations for each. Examples Parameters Lab 13 Therefore, programming languages such as Java offer ways to Using Lists store collections of data in various containers . Class Exercises

  7. Introduction: Arrays Mat 2170 The ArrayList An array is a collection of individual data values Class with two distinguishing characteristics: Week 13 Wrappers 1. An array is ordered Arrays We must be able to count off the values — here is the first, ArrayLists here is the second, and so on — just like Strings . Generic Types Patterns Examples 2. An array is homogeneous Parameters Every value in the array must be of the same type . Lab 13 Using Lists Arrays are a primitive type in Java. Class Exercises They do not have methods associated with them.

  8. Array Terminology Mat 2170 An array is a java container object that holds a fixed The ArrayList Class number of values of a single type . Week 13 The length of an array is established when the array is Wrappers Arrays created. After creation, its length is fixed. ArrayLists Generic Types The individual values in an array are called elements . Patterns Examples The type of object an array can hold is its element type . Parameters Lab 13 Each element is identified by its position in the array Using Lists — also called its index — Class which always begins at 0 and ends at length - 1 Exercises (Just like String objects.)

  9. Array Data Storage Mat 2170 The ArrayList Class The easiest way to visualize arrays is to think of them as a Week 13 linear collection of boxes, much like a row of Post Office Wrappers boxes, each of which is marked with its index number. Arrays For example: ArrayLists Generic Types 0 1 2 3 4 5 6 7 8 9 Patterns intArray : Examples Parameters Lab 13 where intArray was declared as an array of int of size 10. Using Lists Class Exercises

  10. The ArrayList Class Mat 2170 The ArrayList The java.util package includes a class called ArrayList that Class extends the usefulness of arrays by providing additional Week 13 operations and ease of use. Wrappers Arrays The ArrayList class is a wrapper for the primitive array ArrayLists type — it encapsulates an array and provides methods for Generic Types accessing and interacting with it. Patterns Examples Parameters The ArrayList class hides the details of array manipulation. Lab 13 Using Lists Class All operations on an ArrayList object (and hence, the array Exercises within) are accomplished using method calls .

  11. Generic Types and Boxing/Unboxing Mat 2170 The ArrayList Class The element type of any ArrayList must be a Java class . Week 13 Wrappers Automatic conversion of values between a primitive type and Arrays the corresponding wrapper class allows an ArrayList object ArrayLists to store primitive values by using their wrapper classes . Generic Types Patterns Examples For example, to create a list of integers: Parameters ArrayList <Integer> myList = new ArrayList<Integer>(); Lab 13 Using Lists This statement invokes a constructor to create an ArrayList Class of Integer . Exercises

  12. Accessing the Inner int Mat 2170 The ArrayList Then we may store and access int values in myList through Class automatic boxing and unboxing: Week 13 Wrappers Arrays In this statement, Java uses boxing to enclose 42 in a ArrayLists wrapper object of type Integer : Generic Types Patterns myList.add(42); Examples Parameters Here, the statement unboxes the Integer to obtain the int Lab 13 equivalent: Using Lists Class Exercises int answer = myList.get(0);

  13. Back to the ArrayList Class Mat 2170 The ArrayList A new ArrayList object is created by calling the ArrayList Class constructor , for example: Week 13 ArrayList<String> myNames = new ArrayList<String>(); Wrappers Arrays ArrayLists It is a really good idea to specify the element type, such as Generic Types <String> in the example above, in angle brackets when Patterns invoking the constructor. Examples Parameters Lab 13 Doing this allows Java to check for the correct element type Using Lists Class when set() is called, and eliminates the need for a type cast Exercises when get() is called.

  14. Generic Types in Java Mat 2170 The ArrayList Class In the summary of ArrayList methods which follows, the Week 13 notation < T > indicates the base or element type of the Wrappers ArrayList object. Arrays ArrayLists Generic Types In other words, the type parameter < T > is a placeholder Patterns for the element type used in the array. Examples Parameters Lab 13 Class definitions that include a type parameter are called Using Lists generic types . Class Exercises

  15. ArrayList Methods Mat 2170 boolean add(<T> element) The ArrayList Class Adds a new element to the end of the ArrayList ; Week 13 the return value is always true Wrappers void add(int index, <T> element) Arrays Inserts a new element into the ArrayList ; ArrayLists before the position specified by index Generic Types Patterns <T> remove(int index) Examples Removes the element at the specified position and Parameters returns that value Lab 13 Using Lists boolean remove(<T> element) Class Removes the first instance of element , if it appears; Exercises returns true if a match is found

  16. Mat 2170 The ArrayList Class void clear() Week 13 Removes all elements from the ArrayList Wrappers int size() Arrays Returns the number of elements in the ArrayList ArrayLists Generic Types <T> get(int index) Patterns Returns the object at the specified index Examples <T> set(int index, <T> value) Parameters Sets the element at the specified index to the new Lab 13 value and returns the old value Using Lists Class Exercises

  17. Mat 2170 The ArrayList Class indexOf(<T> value) Week 13 Returns the index of the first occurrence of the Wrappers Arrays specified value, or − 1 if it does not appear ArrayLists boolean contains(<T> value) Generic Types Returns true if the ArrayList contains the Patterns specified value Examples boolean isEmpty() Parameters Returns true if the ArrayList contains no elements Lab 13 Using Lists Class Exercises

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