COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
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;
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Chapter 7 – Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
Array Lists Chapter 7.2 Pages Array Lists In Java, Arrays are an important structure for storing data. We will learn more about arrays later,
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Introduction of Concepts Ming Li.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Department of Computer Science
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
DREW ALVAREZ AND CORDIE GOODRICH ARRAYS AND ARRAY LISTS.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter Goals To collect elements using arrays and array lists
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.
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
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.
Data collections Android Club Agenda Array ArrayList HashMap.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
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.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Arrays and ArrayLists. int numbers[] = new int[10]; numbers Other ways to declare the same array 1) int[] numbers = new.
COM S 228 Generics Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Copyright © 2013 by John Wiley & Sons. All rights reserved. ARRAYS and ARRAYLISTS CHAPTER Slides by Donald W. Smith TechNeTrain.com 6 Final Draft 10/30/2011.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Arrays.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
JAC444: Intro to Java Arrays and Vectors Tim McKenna
The Container Class A Collection of Data. What is a Container Class?  A class that can contain a collection of items  A list or bag of items of the.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 6 – Array and Array Lists Two-Dimensional Arrays
Chapter 7 – Arrays and Array Lists
Array Lists An array list stores a sequence of values whose size can change. An array list can grow and shrink as needed. ArrayList class supplies methods.
Department of Computer Science
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Intro to Collections.
Can store many of the same kind of data together
Arrays versus ArrayList
Can store many of the same kind of data together
Grouped Data Arrays, and Array Lists.
Arrays and Array Lists CS 21a.
Can store many of the same kind of data together
Arrays and ArrayLists.
Presentation transcript:

COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University

Disadvantages of Array  When writing a program that collects values, we don’t always know how many values we will have  We will have to manage the size of an array, and the valid length (i.e., lastIndex) ourselves  We need to implement operations such as insert and remove ourselves

ArrayList  Array lists can grow and shrink as needed  The ArrayList class supplies methods for common tasks, such as inserting and removing elements  import java.util.ArrayList

Declare and Use an ArrayList ArrayList friends = new ArrayList (); friends.add(“Cindy”); friends.add(“Scott”); String name = friends.get(1); friends.set(0, “Harry”); Variable type Variable name An array list object of size 0 The add method appends an element to the array list Use get and set methods to access an element Note: For now, use ArrayList only to store String. You cannot add an primitive type of data (etc., int, double)

Some Key Points  ArrayList must be initialized before use  When the ArrayList is initialized, the size of the array is 0  The size increases when you add an object, decreases when you remove an object  The last valid index is names.size()-1  As with arrays, it is an error to access a nonexistent element ArrayList names; names.add(“Harry”); // Error! int i = names.size(); String name = names.get (i); // Error!

Some Key Points  To set an array list element to a new value, use the set method  You can insert an element in the middle of an array list // overwrite the element at position 2 names.set(2, “John”); // add a new element at position 1 // and moves all elements with index // 1 or larger by one position names.add(1, “Scott”);

Some Algorithms // visit all elements of an array list for (int i=0; i<names.size(); i++) { String name = names.get(i); System.out.println(name); } // copy an array list ArrayList newNames = newArrayList (names); // reverse the names list in reverse order, and store // starting with the last element and store ArrayList reversed = new ArrayList (); for (int i=names.size()-1; i>=0; i--) { reversed.add(names.get(i)); }

Exercise What does the array list names contain after the following statements? ArrayList names = new ArrayList ; names.add(“Bob”); names.add(0, “Ann); names.remove(1); names.add(“Cal”);