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.

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;
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
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.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
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.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
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.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Stacks, Queues, and Deques
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Arrays And ArrayLists - S. Kelly-Bootle
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Collections Framework (JCF) Introduction and review 1.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
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.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Object-Oriented Programming Part 3 Bank Account Embezzling Hood College JETT Workshop Dept. of Computer Science February Objectives of this presentation:
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
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.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Objects First With Java A Practical Introduction Using BlueJ Casting Week
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.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
The ArrayList Data Structure The Most Important Things to Review.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
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.
Click to edit Master text styles Stacks Data Structure.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
(like an array on steroids)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Announcements & Review
COP 3503 FALL 2012 Shayan Javed Lecture 8
Programming in Java Lecture 11: ArrayList
ArrayList Collections.
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
slides created by Marty Stepp
ArrayLists 27-Apr-19.
Web Design & Development Lecture 6
TCSS 143, Autumn 2004 Lecture Notes
Arrays and ArrayLists.
Presentation transcript:

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 differences. –Arrays are declared with a set length such as: int grades[]=new int[10]; – An ArrayList would be declared as: List grades=new ArrayList() –The ArrayList is declared with no set length, where a regular array has a set length.

Setting the Elements in Your ArrayList An ArrayList may a set type of object, it cannot store primitive data types unless cast as an object. –ArrayList grade=new ArrayList (); It is required for Java 5.0 that you include a data type, it may also be a class. –ArrayList people=new ArrayList (); This makes keeping track of the type stored in the ArrayList more simple and the program will run more efficiently.

Using Array List as Return Type In a Method public ArrayList getClassList() { // code here } An ArrayList can be returned to a method, this can be very useful when finding possible indexes They may also be a parameter for a method: public int getLocations(ArrayList locs) { //code here } These both can be very useful when writing a long and draw out class.

Types of Methods to Use on an ArrayList size() isEmpty() add(E elmt) add(int i, E elmt) -Returns the number of elements in an ArrayList ***Careful when using this method is returns the number of elements i.e. the length+1. -Returns a Boolean stating whether or not the ArrayList has anything in it. -Returns a Boolean and will add the element at the end of the list and return true. - Inserts elmt into i of ArrayList, shift everything else to the right.

get(int i) set(int I, E elmt) remove(int i) Returns the value in the i element of the ArrayList. Replaces the element in i of ArrayList with elmt Removes the element i in the ArrayList. **decrements size of ArrayList by 1

.get() Method ArrayList test=new ArrayList (); //test is set to{ 4, 10, 16, 3, 18} test.get(2); // will return 16 for(int x=0; x<test.size(); x++) System.out.println(test.get(x));// This will display: //4 //10 //16 //3 //18

.size() Method ArrayList test=new ArrayList (); //test is set to{ 4, 10, 16, 3, 18} int last=test.size();// last is set to the value of 5. //in this line it asks for the element at size test.get(test.size()); //it will return an out of bounds array ERROR. //proper decleration to get the last element in the array would look like //this: test.get(test.size()-1); The bottom method works because size returns the number of elements in the ArrayList not the length of it.

.add() Method ArrayList test=new ArrayList (); //test is set to{ 4, 10, 16, 3, 18} test.add(7); //test is now set to {4, 10, 16, 3, 18, 7} If you don’t use the method which clarifies the position of the object being inserted it will simply add it to the end of an array and increment the length of the ArrayList by one. test.add(3, 19); //test is now set to {4, 10, 16, 19, 3, 18, 7} This method will add the element at the specified index the first parameter of the method specifies where in the ArrayList the second parameter will go.

.remove() Method ArrayList test=new ArrayList (); //test is set to{ 4, 10, 10, 16, 3, 18, } test.remove(3); //sets test equal to{ 4, 10, 10, 3, 18} for(int x=0; x<test.length(); x++) { if(test.get(x)==10) test.remove(x); } This code will NOT work properly. The intention would be to remove all the elements of 10 in the list, but seeing how it changes the length of the array when it runs through the remove method it will not notice the second 10 and return an ArrayList with {4, 10, 16, 3, 18}

.set() Method The set method will take a position in an Array list and an element to replace the one at it currently ArrayList test=new ArrayList (); //test is set to{ 4, 10, 10, 16, 3, 18, } test.set(0, 14); //test is now set to{ 14, 10, 10, 16, 3, 18, } This method is fairly straightforward and simple, it is almost like using a remove and an add method at the same time. It will have occasional uses.

The End This was a quick and brief introduction to the ArrayList (); While this may have covered a very minimal part of what you can do with an ArrayList, you can try them out on BlueJ yourself and test and see the possibilities with an ArrayList.