Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.

Slides:



Advertisements
Similar presentations
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
© A+ Computer Science - ArrayList © A+ Computer Science - Lab 16.
Building Java Programs
List Interface CS java.util.List Interface and its Implementers CS340 2.
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.
Arrays And ArrayLists - S. Kelly-Bootle
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
GENERICS. Generics  Classes and methods can have a type parameter (actually, more than one).  The type parameter can be any reference (class) type.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
© A+ Computer Science - Which of the following statements assigns the letter S to the third row and first column of a two-dimensional.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Chapter Objectives  The List interface  Implement lists based on arrays  Learn about List applications CS340 1.
List Interface and Linked List Mrs. Furman March 25, 2010.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.
The ArrayList Data Structure The Most Important Things to Review.
The ArrayList Data Structure Standard Arrays at High Speed!
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
(like an array on steroids)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Welcome to CSE 143!.
Array List Pepper.
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
ArrayLists.
ArrayLists.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
ArrayList Collections.
Chapter 10 ArrayList reading: 10.1
ArrayLists.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Java Arrays & Strings.
Welcome to CSE 143! Go to pollev.com/cse143.
Grouped Data Arrays, and Array Lists.
Methods Copying Autoboxing
Lecture 13: ArrayLists AP Computer Science Principles
Code Refresher Test #1 Topics:
ArrayLists 22-Feb-19.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists.
Parsing JSON, Using Libraries, Java Collections, Generics
slides created by Marty Stepp
ArrayLists 27-Apr-19.
Presentation transcript:

Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008

Wrapper Classes Used to make primitive data into objects! Used to make primitive data into objects! We are using Integer to make ints objects We are using Integer to make ints objects We are using Double to make doubles objects. We are using Double to make doubles objects. We primarily we are going to use this with ArrayList, because we can only store objects in an ArrayList!! We primarily we are going to use this with ArrayList, because we can only store objects in an ArrayList!!

Integer Class Integer myInt = new Integer (3); //declares and creates myInt as an Integer, and stores 3. int i = myInt.intValue(); //unwrapped the 3, and assign it to i.

Double Class Double myDouble = new Double (1.5); Double myDouble = new Double (1.5); // Create an object of type Double to store 1.5 double d = myDouble.doubleValue();

ArrayList Can only store OBJECTS!!! Can only store OBJECTS!!! Size is not a constant, we can resize as we go. Size is not a constant, we can resize as we go. inserts into the ArrayList easily, deletes from ArrayList inserts into the ArrayList easily, deletes from ArrayList

ArrayList Methods NameUse size() Returns the number of elements in the List add(Object) Appends an Object to the end of the List add (index, Object) Inserts an Object into the List at the given index. Every other object is shifted down. get (index) Returns the Object at the given index set (index, Object) Replaces the Object at the given index with the new Object remove(index) Removes the Object at the index, and shifts all Objects to the right down 1.

Constructing an ArrayList ArrayList myList = new ArrayList(); boolean add(Object o) boolean tf = myList.add(new Integer(3)); boolean tf = myList.add(new Integer (5)); Elements are added to the end of the list… 3 5

Add all the values in the ArrayList int sum = 0; Integer myInt; for (int i = 0; i<myList.size(); i++) { myInt = ((Integer)(myList.get(i)).intValue(); sum += myInt; }

Add Integer Objects int sum = 0; Integer i = new Integer (4); Integer j = new Integer (6); sum = i + j;

get(i) The return type of get is an Object.. so you need to typecast to get it to the correct type. myInt = ((Integer)(myList.get(i)).intValue(); typecast ArrayList method (parameter) method () typecast ArrayList method (parameter) method () ArrayList Integer Store multiple types in an ArrayList…

.add(index, obj) void add(int index, E obj) myList.add(0, new Integer (7)); We can add to our list at the index one more that what we currently have, and it will resize, if we try to add well beyond that… myList.add (15, new Integer (15)); it will error.

Inserting into an ArrayList myList.add (4, new Integer (13));

.add(Object) Appends the object to the end of the list. Example: myList.add ( new Integer (13));

.remove (index) Integer myInt = myList.remove (4); //removes 10 from the list…stores 10 in myInt

.set(index, Object) Integer myInt = myList.set (4, new Integer(8)); //8 is add to the list, 10 is stored in myInt

Example ArrayList ray; ray = new ArrayList (); 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]

Example 2 ArrayList ray; ray = new ArrayList (); ray.add("a"); ray.add("b"); ray.remove(0); ray.add("c"); ray.add("d"); ray.remove(0); System.out.println(ray); Output: [c, d ]

Array Vs. ArrayList GCOC – APCS A ArrayListArray ArrayList myList = new ArrayList (); String [ ] myList = new String[2]; String a = new String(“woohoo”); myList.add(a); String a = new String(“woohoo”); myList[0] = a; String b = new String(“Frog”); myList.add(b); String b = new String(“Frog”); myList[1] = b;

Array Vs. ArrayList GCOC – APCS A ArrayListArray int theSize = myList.size();int theSize = myList.length; Object o = myList.get(1); -or- String o = (String )myList.get(1); String o = myList[1]; myList.remove(1);myList[1] = null;

For Each Loops For Each loops are great to use with Arrays, ArrayList, 2D Arrays, etc. For Each loops are great to use with Arrays, ArrayList, 2D Arrays, etc. Use whenever you want to loop from the beginning to the end of a list. Use whenever you want to loop from the beginning to the end of a list. Do not use if you do not want to search the entire list. Do not use if you do not want to search the entire list.

For Each Structure for (Object o : arrayName) { Type of Object stored Name of the collection } Example: For Each int[] data = {3, 5, 7}; int sum = 0; for (int x: data) { sum += x; } Example: Regular For int[] data = {3, 5, 7}; int sum = 0; for (int i = 0; i < data.length; i++) { sum += data[i]; sum += data[i];}