The ArrayList Data Structure The Most Important Things to Review.

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;
Hip Hip Array! AP Computer Science. Remember Strings? Strings are an array of characters An array is a collection of variables all of the same type. Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Arrays.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Arrays And ArrayLists - S. Kelly-Bootle
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
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.
Chapter 8: Arrays.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
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.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
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.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Chapter 12 The ArrayList Data Structure Section 1 - How to Instantiate an ArrayList Section 2 - The ArrayList Subset Section 3 - Declaring an ArrayList.
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();
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Chapter 9 Introduction to Arrays Fundamentals of Java.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Java Array Lists 2/13/2017.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Lecture 2: Implementing ArrayIntList reading:
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
" A list is only as strong as its weakest link. " - Donald Knuth
Arrays versus ArrayList
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
Object Oriented Programming in java
Grouped Data Arrays, and Array Lists.
Java Array Lists 2/13/2017.
ArrayLists 22-Feb-19.
Review: libraries and packages
Just Enough Java 17-May-19.
List Interface ArrayList class implements the List Interface
Arrays.
Presentation transcript:

The ArrayList Data Structure The Most Important Things to Review

The Java ArrayList Class An ArrayList is a one-dimensional array. An ArrayList holds only objects, not primitive values like int, double, char or boolean. The ArrayList will be created for a beginning amount and then it will automatically resize itself when it becomes full and more elements need to be added.

Instantiating an ArrayList Declaring and instantiating an ArrayList: ArrayList nums = new ArrayList ( ); No square bracket [ ] notation is used for an ArrayList. Note the use of the templating angle brackets that enclose the type of object to be placed in the array. Instead of using [] to manipulate elements, methods are called to perform operations. An ArrayList does have indices and the positions of the elements range from index 0 to index logical size - 1.

Instantiating an ArrayList Declaring and instantiating an ArrayList of other types: ArrayList nums = new ArrayList ( ); ArrayList names = new ArrayList ( ); ArrayList students = new ArrayList ( ); ArrayList shapes = new ArrayList ( ); Note the empty ( ) parentheses at the end of the constructor call. Don’t forget to include these.

The ArrayList Subset of Methods ArrayList methodWhat the Operation Does int size ( )returns the logical size of the list boolean add (E obj)appends the object obj to the end of the list and returns true if successful (the fact that it returns true may be helpful sometimes) void add (int index, E obj)inserts the object obj at position index where index fulfills the expression 0 ≤ index ≤ size. If index is out of bounds in the range of index size(), then Java throws an IndexOutOfBoundsException. Once obj is inserted, then the elements at position index and higher are moved to the right so that 1 is added to their indices and then the logical size is adjusted. Note: if there are 3 elements in the list at indices 0, 1, and 2, then obj can be added at index 3 without an out of bounds error E get (int index)returns the element at position index in the list E set (int index, E obj)replaces the element at position index with obj and returns the element formerly at the specified position E remove (int index)removes the element from position index in the list and then moves all elements at position index + 1 and higher to the left … subtracting 1 from their indices and then adjusting the logical size. The element formerly at the specified position is returned

Adding Elements to an ArrayList - add(obj) The following code stores the first 100 multiples of 3 in nums in order: ArrayList nums = new ArrayList ( ); int value = 3; for (int i = 0; i < 100; i++) { nums.add(value); // adds at the end of the list value += 3; } Note: if nums was a standard array, we would use … nums [i] = value; in place of nums.add(value);

Getting Elements from an ArrayList - get(i) The following code prints the values stored in nums with 10 values per line with a field width of 5: for (int i = 0; i < nums.size(); i++) { if ( (i + 1) % 10 == 0 ) System.out.printf( “%5d%n”, nums.get(i) ); else System.out.printf( “%5d”, nums.get(i) ); } Note: if nums was a standard array, we would use … System.out.printf( “%5d”, nums[i] );

Getting Elements from an ArrayList - get(i) To obtain the first element in an ArrayList always use … nums.get(0); To obtain the last element in an ArrayList always use … nums.get(nums.size() - 1); Note that since there are num.size() elements in the ArrayList, then the index of the first element is 0 and the index of the last element is nums.size() - 1.

Removing Elements from a Position - remove(i) The following code removes and prints all of the multiples of 6 from nums in a field width of 5: int i = 0; while ( i < nums.size() ) { if ( nums.get(i) % 6 == 0 ) { int x = nums.remove(i) ; System.out.printf( “%5d”, x ); // don’t increment i because elements are shifted down } else // if not evenly divisible by 6 increment i i++; }

Removing Elements from a Position - remove(i) The following code removes and prints all of the multiples of 6 from nums in a field width of 5 using a for loop: for (int i = 0; i < nums.size() ; i++) { if ( nums.get(i) % 6 == 0 ) { int x = nums.remove(i) ; System.out.printf( “%5d”, x ); i--; // to cancel the effect of i++ in loop header }

Removing Elements from a Position - remove(i) To delete the first element in an ArrayList always use … nums.remove(0); To delete the last element in an ArrayList always use … nums. remove(nums.size() - 1); Again, since there are num.size() elements in the ArrayList, then the index of the first element is 0 and the index of the last element is nums.size() - 1.

Adding Elements at a Position - add(i, obj) The following code adds the integer 3 at the first of the list named nums using the add(i, obj) method, where the list initially contains 6, 9, 12. nums.add (0, 3); // add 3 at index 0. The list now contains: The following code adds the integer 15 at the end of the list no matter how many elements there are using the add(i, obj) method. nums.add (nums.size(), 15); // add 15 after the last element Since nums.size() is 4 before adding 15, then 15 is added at index 4. Obviously 3, 6, 9, &12 are in indices 0, 1, 2, & 3. The list now contains:

Replacing Elements at a Position - set(i, obj) Assume nums contains: The following code replaces the element at index 2 with the value 10 using the set(i, obj) method. It returns the replaced element and prints it out. int x = nums.set (2, 10); // replace 9 at index 2 with 10. System.out.println(“The replaced value was ” + x); The list now contains: Replacing the last value in the ArrayList … int x = nums.set (nums.size() - 1, 20); The list now contains: