Chapter 13 Array Lists and Arrays. 2 ArrayList Basics Definition: An array list is a sequence of objects Construct an array List object, i.e. in the Purse.java.

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Introduction to C Programming
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.
The ArrayList Class and the enum Keyword
Two Dimensional Arrays and ArrayList
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;
Generics, Lists, Interfaces
Computer Science 209 Software Development Equality and Comparisons.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Chapter 13 ARRAY LISTS AND ARRAYS. CHAPTER GOALS To become familiar with using array lists to collect objects To learn about common array algorithms To.
Chapter 7 Arrays and Array Lists. Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the.
Chapter 13 ARRAY LISTS AND ARRAYS CHAPTER GOALS –To become familiar with using array lists to collect objects –To learn about common array algorithms –To.
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
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[]
Arrays CSC 171 FALL 2002 LECTURE 20. Arrays Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?
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.
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.
Alice in Action with Java
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
© 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
DREW ALVAREZ AND CORDIE GOODRICH ARRAYS AND ARRAY LISTS.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
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]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
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: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
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.
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.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
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.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
MCQ Which is the correct syntax for placing the string "boat" into an ArrayList name recVehicles in position 3 (index 2) for the first time? a)recVehicles.set(3,
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
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];
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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 9 Introduction to Arrays Fundamentals of Java.
Java Programming Language Lecture27- An Introduction.
Principles of Computer Science I
Arrays.
Chapter 8 – Arrays and Array Lists
Chapter 8 Slides from GaddisText
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Arrays and ArrayLists.
Presentation transcript:

Chapter 13 Array Lists and Arrays

2 ArrayList Basics Definition: An array list is a sequence of objects Construct an array List object, i.e. in the Purse.java ArrayList coins = new ArrayList(); Store variable number of objects coins.add(new Coin(0.1, "dime")); Retrieving Array List Elements: get and cast Coin c = (Coin) coins.get(i); Note: if int n = coins.size(); c = (Coin)coins.get(n); // ERROR reason: index values are 0...n-1, bounds errors

3 Several Methods for ArrayList Java API coins.add(Object o) coins.set(int index, Object o) coins.get(int index) coins.add(int index, Object o) coins.set(int index, Object o) Coins.remove(int index)

4 Simple Array List Algorithms Searching a value public boolean search(Coin aCoin) { } Counting # of coins that match given aCoin public int count(Coin aCoin){ } Finding the Maximum public Coin getMaximum{ } Adding Coins public void addCoins(Coin coinType, int count){ }

5 Stepping Through all Elements for (int i = 0; i < coins.size(); i++){ Coin c = (Coin)coins.get(i); do something with c } Public int count(Coin aCoin){ int matches = 0; for (int i = 0; i < coins.size(); i++){ Coin c = (Coin)coins.get(i); if(c.equals(aCoin)) matches++; //found a match } return matches; }

6 Methods from Homework public void addCoins(Coin aCoin) inserts aCoin in its proper place in the array list coins. assume that coins is already sorted in the increasing order of the value of the coins. public String listTheContents() returns a string of the values of the coins of the purse. public void remove( Coin aCoin, int count) removes count occurrences of aCoin from the array list coins. If coins does not have count occurrences of aCoin, the method throws an IllegalArgumentException.

7 Storing Numbers in Array List Array lists store objects Use wrapper classes to store numbers Double wrapper = new Double(29.95); double unwrapped = wrapper.doubleValue() ArrayList data = new ArrayList(); data.add(wrapper); unwrapped = ((Double).data.get(i)).doubleValue(); Also have Integer and Boolean wrappers

8 Arrays Definition: An array is a fixed length sequence of values of the same type. How do you declare arrays? double[] data = new double[10]; How do you work with arrays? by specifying the subscript, i.e., coin[] pocket = {dime, quarter, dollar, nickel}; Coin aCoin = pocket[2]; //aCoin is dollar length field stores the number of elements in an array.

9 Copying Arrays Copy & Clone System.arrayCopy(fromArray, fromIndex, toArray, toIndex, size) When will you use array copy when you want to add/remove an item Insert: System.arrayCopy(a, i, a, i+1, a.length-i-1); a[i] = x; Remove: System.arrayCopy(a, i+1, a, i, a.length-i-1);

10 Compare ArrayList and Array Array ListArrays DefinitionA sequence of objectsAn array is a fixed length sequence of values of the same type Object typeCould be differentSame type Construct ArrayList coins = new ArrayList(); double[] data = new double[10]; # of elements coins.size() increases, shrinks a.length fixed index[0, coins.size()-1][0, a.length-1] access/ retrieve Get and cast, Coin c = (Coin)coins.get(i); element type followed by index, i.e., Coin aCoin = pocket[i]; updateadd, remove, setAssignment to individual element method typenon-staticstatic

11 Problem Statement for DataSet Partially filled arrays Companion variable to tell how many elements in the array are actually used. data.size() is the capacity of the array data, while dataSize is the current size of the array. Two java files DataSet.java DataSetTest.java