ArrayLists 22-Feb-19.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
11-Jun-15 Generics. A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Arrays And ArrayLists - S. Kelly-Bootle
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
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.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
List Interface and Linked List Mrs. Furman March 25, 2010.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
EKT472: Object Oriented Programming
(like an array on steroids)
Linked Lists Chapter 5 (continued)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists.
Programming in Java Lecture 11: ArrayList
ArrayLists.
Generics 27-Nov-18.
(Java Collections Framework) AbstractSequentialList
ArrayList Collections.
Java Arrays & Strings.
Object Oriented Programming in java
Grouped Data Arrays, and Array Lists.
Collections Framework
Review of Previous Lesson
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Searching.
Linked Lists Chapter 5 (continued)
ArrayLists.
ArrayLists 27-Apr-19.
slides created by Alyssa Harding
Review: libraries and packages
Generics 2-May-19.
ArrayList.
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
Arrays and ArrayLists.
Linked Lists Chapter 5 (continued)
What can you put into an ArrayList?
Presentation transcript:

ArrayLists 22-Feb-19

Arrays in Java Java has built in arrays as well as more complicated classes to automate many array tasks (the ArrayList class) arrays hold elements of the same type primitive data types or classes space for array must be dynamically allocated with new operator. (Size is any integer expression. Due to dynamic allocation does not have to be a constant.) AP Computer Science

public void arrayExamples() { int[] intList = new int[10]; for(int i = 0; i < intList.length; i++) { assert 0 >= i && i < intList.length; intList[i] = i * i * i; } intList[3] = intList[4] * intList[3];

Array Details all arrays must be dynamically allocated arrays have a public, final field called length built in size field, no separate variable needed don't confuse length (capacity) with elements in use elements start with an index of zero, last index is length - 1 AP Computer Science

Array Initialization Array variables are object variables They hold the memory address of an array object The array must be dynamically allocated All values in the array are initialized (0, 0.0, char 0, false, or null) Arrays of primitives and Strings may be initialized with an initializer list: int[] intList = {2, 3, 5, 7, 11, 13}; double[] dList = {12.12, 0.12, 45.3}; String[] sList = {"Olivia", "Kelly", "Isabelle"}; AP Computer Science

Arrays of objects A native array of objects is actually a native array of object variables all object variables in Java are really what? Pointers! public void objectArrayExamples() { Rectangle[] rectList = new Rectangle[10]; // How many Rectangle objects exist? rectList[5].setSize(5,10); //uh oh! for(int i = 0; i < rectList.length; i++) { rectList[i] = new Rectangle(); } rectList[3].setSize(100,200); AP Computer Science

ArrayLists and arrays A ArrayList is like an array of Objects Differences between arrays and ArrayLists: Arrays have special convenience syntax; ArrayLists don’t An array is a fixed size, but a ArrayList expands as you add things to it This means you don’t need to know the size beforehand Arrays can hold primitives or objects, but ArrayLists can only hold objects However, autoboxing can make it appear that an ArrayList can hold primitives

Arrays in Java The ArrayList Class A class that is part of the Java Standard Library and a class that is part of the AP subset a kind of automated array not all methods are part of the ap subset AP Computer Science

About Lists (in general) Arrays in Java About Lists (in general) A list is an ordered collection or a sequence. ArrayList implements the List interface The user of this interface will have control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list. Items can be added, removed, and accessed from the list AP Computer Science

Methods ArrayList() //constructor void add(int index, Object x) Arrays in Java Methods ArrayList() //constructor void add(int index, Object x) boolean add(Object x) Object set(int index, Object x)               Object remove(int index) int size () Object get(int index)   Iterator iterator()                  AP Computer Science

How the methods work add: Arrays in Java boolean add(Object x) – inserts the Object x at the end of the list (size increases by 1), returns true void add(int index, Object x) – inserts the Object x at the given index position (elements will be shifted to make room and size increases by 1) AP Computer Science

How the methods work get: Arrays in Java returns the Object at the specified index should cast when using value returned throws IndexOutOfBoundsException if index<0 or index>=size AP Computer Science

How the methods work set Arrays in Java replaces value of Object parameter at the given index size is not changed AP Computer Science

How the methods work remove Arrays in Java removes the element at the specified index throws IndexOutOfBoundsException if index<0 or index>=size size will be decreased by 1 returns Object removed AP Computer Science

Creating a ArrayList the new way Specify, in angle brackets after the name, the type of object that the class will hold Examples: ArrayList<String> vec1 = new ArrayList<String>(); ArrayList<String> vec2 = new ArrayList<String>(10); To get the old behavior, but without the warning messages, use the <?> wildcard Example: ArrayList<?> vec1 = new ArrayList<?>();

Adding elements to a ArrayList boolean add(Object obj) Appends the object obj to the end of this ArrayList With generics, the obj must be of the correct type, or you get a compile-time (syntax) error Always returns true This is for consistency with other, similar classes void add(int index, Object element) Inserts the element at position index in this ArrayList The index must be greater than or equal to zero and less than or equal to the number of elements in the ArrayList

Removing elements boolean remove(Object obj) void remove(int index) Removes the first occurrence of obj from this ArrayList Returns true if an element was removed Uses equals to test if it has found the correct element void remove(int index) Removes the element at position index from this ArrayList void clear() Removes all elements

Accessing with and without generics Object get(int index) Returns the component at position index Using get the old way: ArrayList myList = new ArrayList(); myList.add("Some string"); String s = (String)myList.get(0); Using get the new way: ArrayList<String> myList = new ArrayList<String>(); myList.add("Some string"); String s = myList.get(0); Notice that casting is no longer necessary when we retrieve an element from a “genericized” ArrayList

Searching a ArrayList boolean contains(Object element) Tests if element is a component of this ArrayList Uses equals to test if it has found the correct element int indexOf(Object element) Returns the index of the first occurrence of element in this ArrayList Returns -1 if element was not found in this ArrayList int lastIndexOf(Object element) Returns the index of the last occurrence of element in this ArrayList

Getting information boolean isEmpty() int size() Object[ ] toArray() Returns true if this ArrayList has no elements int size() Returns the number of elements currently in this ArrayList Object[ ] toArray() Returns an array containing all the elements of this ArrayList in the correct order

More about equals There are many different notions of equality Example: two sets are equal if they contain the same elements; order of elements is irrelevant Java defines public boolean equals(Object) in the Object class, but equals is defined to be the same as == It’s often a good idea to override equals for your own objects If you do this, note that the argument should be a general Object The String class (and some others) override equals

Conclusion A ArrayList is like an array of Objects The advantage of a ArrayList is that you don’t need to know beforehand how big to make it The disadvantage of a ArrayList is that you can’t use the special syntax for arrays You should never use an array that you hope is “big enough”—use a ArrayList instead