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.

Slides:



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

Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
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.
Chapter 10 Introduction to Arrays
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Chapter 7 – Arrays.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Building Java Programs
Chapter 9 Introduction to Arrays
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
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.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
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.
Chapter 11 Arrays Continued
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.
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 An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
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.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
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.
Chapter 10 – ArrayList s and an Introduction to the Java Collections Framework The ArrayList Class How to Create an ArrayList Object Adding Elements to.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
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!
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.
CSE 1201 Object Oriented Programming ArrayList 1.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
CS 116: Object Oriented Programming II. Topics Vectors Multidimensional Arrays ArrayList.
The ArrayList Data Structure The Most Important Things to Review.
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.
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
CMSC 202 ArrayList Aug 9, 2007.
Sixth Lecture ArrayList Abstract Class and Interface
(like an array on steroids)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
COP 3503 FALL 2012 Shayan Javed Lecture 8
The ArrayList Class An ArrayList object stores a list of objects, and is often processed using a loop The ArrayList class is part of the java.util package.
Programming in Java Lecture 11: ArrayList
ArrayList Collections.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
Object Oriented Programming in java
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
Collections Framework
slides created by Alyssa Harding
Chapter 2 : List ADT Part I – Sequential List
List Interface ArrayList class implements the List Interface
Arrays and ArrayLists.
Presentation transcript:

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 sequence of elements that are ordered by position. An ArrayList tracks both its logical size (the number of elements that are currently in it) and its physical size (the number of cells available to store elements). An ArrayList tracks both its logical size (the number of elements that are currently in it) and its physical size (the number of cells available to store elements). When a programmer creates an array list, its logical size is 0. When the programmer inserts elements into an array list, the list automatically updates its logical size and adds cells to accommodate new objects if necessary. The list’s logical size is also updated when an element is removed. When a programmer creates an array list, its logical size is 0. When the programmer inserts elements into an array list, the list automatically updates its logical size and adds cells to accommodate new objects if necessary. The list’s logical size is also updated when an element is removed.

ArrayList cont… The programmer must use methods rather than the subscript operator [] to manipulate elements in an array list. The programmer must use methods rather than the subscript operator [] to manipulate elements in an array list. The positions available for access in an array list range from 0 to its logical size minus 1. The positions available for access in an array list range from 0 to its logical size minus 1. Must include the following import: Must include the following import: import java.util.ArrayList; import java.util.ArrayList;

Advantages of an ArrayList over an Array An ArrayList shrinks and grows as needed in a program, whereas an array has a fixed length that is set when the array is created. An ArrayList shrinks and grows as needed in a program, whereas an array has a fixed length that is set when the array is created. In an ArrayList list, the last slot is always list.size() – 1, whereas in a partially filled array, you, the programmer, must keep track of the last slot currently in use. In an ArrayList list, the last slot is always list.size() – 1, whereas in a partially filled array, you, the programmer, must keep track of the last slot currently in use. For an ArrayList, you can do insertion or deletion with just a single statement. Any shifting of elements is handled automatically. In an array, however, insertion or deletion requires you to write the code that shifts the elements. For an ArrayList, you can do insertion or deletion with just a single statement. Any shifting of elements is handled automatically. In an array, however, insertion or deletion requires you to write the code that shifts the elements.

ArrayList Methods ArrayList Method What it does ArrayList<E>() Constructor builds an empty array list boolean isEmpty() Returns true if the list contains no elements and false otherwise int size() Returns the number of elements currently in the list E get(int index) Returns the element at index E set(int index, E obj) Replaces the element at index with obj and returns the old element void add(int index, E obj) Inserts obj before the element at index or after the last element if index equals the size of the list E remove(int index) Removes and returns the element at index int indexOf(E obj) Returns the index of the first instance of obj in the list or -1 if obj is not in the list

Elements of an ArrayList The elements in an ArrayList must all be objects. Thus, the following attempt to declare and create an ArrayList of int fails with a syntaz error: The elements in an ArrayList must all be objects. Thus, the following attempt to declare and create an ArrayList of int fails with a syntaz error: ArrayList list = new ArrayList (); //Invalid (syntax error) ArrayLists cannot contain primitive types ArrayLists cannot contain primitive types

Wrapper Classes Allows us to store primitive data types in array lists. Allows us to store primitive data types in array lists. A wrapper class is a class that contains a value of a primitive type. A wrapper class is a class that contains a value of a primitive type. Values of the types boolean, char, int and double can be stored in objects of the wrapper classes Boolean, Character, Integer, and Double respectively. Values of the types boolean, char, int and double can be stored in objects of the wrapper classes Boolean, Character, Integer, and Double respectively. In order to use a primitive data type with an ArrayList you must use the appropriate wrapper class name as the element type specifier when declaring the ArrayList variable and instantiating the list. In order to use a primitive data type with an ArrayList you must use the appropriate wrapper class name as the element type specifier when declaring the ArrayList variable and instantiating the list.

Test an ArrayList of integers import java.util.ArrayList; public class TestArrayList{ public static void main(String [] args){ //Create a list of Integers ArrayList list = new ArrayList (); //Add the ints to the list for (int i = 0; i < 100; i++) list.add(i); //Increment each int in the list for (int i = 0; i < list.size(); i++) list.set(i, list.get(i) + 1); //Display the contents of the list for (int i = 0; i < 100; i++) System.out.println(list.get(i));}}

Enhanced for loop/for-each loop Used to iterate over an array or collection(ArrayList) Used to iterate over an array or collection(ArrayList) General Form General Form for(SomeType element : collection) {statements}

Enhanced for loop example public class Enhanced { public static void main(String [] args) { //declaration and initialization of array int[] array = {5, 4, 6, 3, 2, 9}; //outputs all elements of array for(int x : array) System.out.print(x + “ – “); }}//output 5 – 4 – 6 – 3 – 2 – 9 –