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.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

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;
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
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.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
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
Chapter 3 Introduction to Collections – Stacks Modified
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
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.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
Object Oriented Programming in Java Habib Rostami Lecture 7.
The ArrayList Data Structure Standard Arrays at High Speed!
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
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.
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
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
Generics 27-Nov-18.
ArrayList Collections.
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
Collections Framework
Review of Previous Lesson
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists.
ArrayLists 27-Apr-19.
Generics 2-May-19.
ArrayList.
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
Presentation transcript:

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 convenience syntax; ArrayList s 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 ArrayList s can only hold objects However, autoboxing can make it appear that an ArrayList can hold primitives

3 Generics In Java versions 1.4 and earlier, an ArrayList just held Object s, which could be of any (non-primitive) type For compatibility reasons you can still do this, but you will get a lot of warning messages In Java 5 you should specify the class of objects that the ArrayList will hold This is done with the new generics syntax

4 Creating a ArrayList the old way The syntax for creating ArrayList s has changed between Java 1.4 and Java 5 For compatibility reasons, the old way still works, but will give you warning messages Here are the (old) constructors: import java.util.ArrayList; ArrayList vec1 = new ArrayList(); Constructs an ArrayList with an initial capacity of 10 ArrayList vec2 = new ArrayList( initialCapacity );

5 Using an ArrayList the old way boolean add(Object obj ) Appends the object obj to the end of this ArrayList Example: myArrayList.add("A string is an object"); Object get(int index ) Returns the component at position index Note that this is returned as an Object ; to use it as a String, you must cast it Example: String s = (String)myArrayList.get(5);

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

7 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 With generics, the obj must be of the correct type, or you get a compile-time (syntax) error

8 Removing elements boolean remove(Object obj ) 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

9 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 myList = new ArrayList (); 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

10 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 Uses equals to test if it has found the correct element 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 Uses equals to test if it has found the correct element Returns -1 if element was not found in this ArrayList

11 Getting information boolean isEmpty() 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

12 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

13 Conclusion A ArrayList is like an array of Object s 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

14 The End “Where a calculator on the ENIAC is equipped with vacuum tubes and weighs 30 tons, computers of the future may have only vacuum tubes and perhaps weigh 1½ tons.” --Popular Mechanics, March 1949