ArrayLists 27-Apr-19.

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

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.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
ArrayList, Multidimensional Arrays
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.
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.
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.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
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.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
EKT472: Object Oriented Programming
Searching.
(like an array on steroids)
Lecture 3 Linear Search and Binary Search ArrayLists
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Efficiency of in Binary Trees
COP 3503 FALL 2012 Shayan Javed Lecture 8
Arrays and the ArrayList Class The ArrayList Class
Welcome to CSE 143!.
Array List Pepper.
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists.
Programming in Java Lecture 11: ArrayList
Introduction to Collections
Generics 27-Nov-18.
(Java Collections Framework) AbstractSequentialList
EE 422C Sets.
Welcome to CSE 143!.
Java Arrays & Strings.
Object Oriented Programming in java
Grouped Data Arrays, and Array Lists.
Introduction to Collections
ArrayLists 22-Feb-19.
Collections Framework
slides created by Marty Stepp
Introduction to Collections
Review of Previous Lesson
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Searching.
Arrays.
ArrayLists.
slides created by Alyssa Harding
Generics 2-May-19.
ArrayList.
TCSS 143, Autumn 2004 Lecture Notes
slides created by Ethan Apter and Marty Stepp
Part of the Collections Framework
Arrays and ArrayLists.
Presentation transcript:

ArrayLists 27-Apr-19

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 We will discuss autoboxing in some other lecture

Creating a ArrayList 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); (10 is the initial size you want) This is sort of like an array, where you would say String[] ary = new String[10]; instead of ArrayList<String> ary = new ArrayList<String>(10);

Adding elements to a ArrayList boolean add(Type obj) Appends the object obj to the end of this ArrayList 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, Type 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

Getting values out Type get(int index) Using get: Returns the component at position index Using get: ArrayList<String> myList = new ArrayList<String>(); myList.add("Some string"); String s = myList.get(0);

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() Returns true if this ArrayList has no elements int size() Returns the number of elements currently in this ArrayList Type[ ] toArray(Type[ ]) Returns an array containing all the elements of this ArrayList in the correct order The parameter is usually a zero-length array of the correct type Example: String[] names = nameList.toArray(new String[0]);

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

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