Array Lists Chapter 7.2 Pages 248 - 253. Array Lists In Java, Arrays are an important structure for storing data. We will learn more about arrays later,

Slides:



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

11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
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;
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
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[]
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Building Java Programs
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
ICOM 4015: Advanced Programming Lecture 7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Seven:
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 6 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
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];
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
The ArrayList Data Structure Standard Arrays at High Speed!
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 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Chapter 6 – Array and Array Lists Two-Dimensional Arrays
Chapter 7 – Arrays and Array Lists
Array Lists 7.7.
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.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Chapter 8 Arrays, Strings and pointers
Intro to Collections.
ARRAYLIST AND VECTOR.
Arrays and the ArrayList Class The ArrayList Class
Array List Pepper.
ArrayLists.
Object Oriented Programming in java
Introduction to Data Structure
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Chapter 2 : List ADT Part I – Sequential List
Presentation transcript:

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, but for now, you need to know: An array is a collection of similar objects that are stored sequentially in the same block of memory. Object1 Object2 Object3 Object4

Array Lists An Array’s items are stored sequentially in a block memory (RAM). RAM stands for Random Access Memory – Random Access means that any random (arbitrary) block of memory can be accessed simply specifying the memory address. – In English, this means you do not have to scan RAM sequentially to find stuff, like we did with files.

Array Lists Arrays and RAM work to allow you to access (“jump”) to almost any memory location. By placing objects together in memory, you can jump directly to a specific object. If you know – The start of the array and – the size of the object, you could jump to the – first object, – fifth object, or – nth object.

File vs. RAM File  RAM 0     … 4816 

Lists We all know what a list is, right? – A List is a series of items that are ordered. In Java, a List is an interface. – An interface describes a set of actions on a data structure

List Actions (methods) add – An item to the list remove – An item specified by its value – An item specified by its index (order number) contains – Does the list contain an item? size of the list get – Item at index indexOf – The item you are searching for

Array Lists An ArrayList is class that implements the List interface using Arrays as the underlying data structure. Lists could be implemented with files or pointers Arrays are great way to implement lists

Array Lists ArrayList class manages a sequence of objects Can grow and shrink as needed ArrayList class supplies methods for many common tasks, such as inserting and removing elements ArrayList is a generic class: ArrayList collects objects of type parameter T : ArrayList names = new ArrayList (); names.add("Emily"); names.add("Bob"); names.add("Cindy"); size method yields number of elements

To add an object to the end of the array list, use the add method: names.add("Emily"); names.add("Bob"); names.add("Cindy"); Adding Elements

To obtain the value an element at an index, use the get method Index starts at 0 String name = names.get(2); // gets the third element of the array list Bounds error if index is out of range Most common bounds error: int i = names.size(); name = names.get(i); // Error // legal index values are 0... i-1 Retrieving Array List Elements

To set an element to a new value, use the set method: names.set(2, " Carolyn " ); Setting Elements

To remove an element at an index, use the remove method: names.remove(1); Removing Elements

To remove an element at an index, use the remove method: if (names.contains(“Carolyn”)) System.out.println(“Carolyn is here”); Contains

names.add("Emily"); names.add("Bob"); names.add("Cindy"); names.set(2, "Carolyn"); names.add(1, "Ann"); names.remove(1); Adding and Removing Elements

Working with Array Lists ArrayList names = new ArrayList (); Constructs an empty array list that can hold strings. names.add("Ann"); names.add("Cindy"); Adds elements to the end. System.out.println(names); Prints [Ann, Cindy]. names.add(1, "Bob"); Inserts an element at index 1. names is now [Ann, Bob, Cindy]. names.remove(0); Removes the element at index 0. names is now [Bob, Cindy]. names.set(0, "Bill"); Replaces an element with a different value. names is now [Bill, Cindy].

Working with Array Lists (cont.) String name = names.get(i); Gets an element. String last = names.get(names.size() - 1); Gets the last element. ArrayList squares = new ArrayList (); for (int i = 0; i < 10; i++) { squares.add(i * i); } Constructs an array list holding the first ten squares.

Syntax 7.2 Array Lists