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:



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

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. 2 ArrayList Basics Definition: An array list is a sequence of objects Construct an array List object, i.e. in the Purse.java.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7: Arrays and Array Lists. To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the generalized.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
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.
Chapter 7 – Arrays.
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,
Chapter 7  Arrays and Array Lists 1 Chapter 7 Arrays and Array Lists.
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.
Alice in Action with Java
C++ for Engineers and Scientists Third Edition
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Java Programming Week 6: Array and ArrayList Chapter 7.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
ICOM 4015: Advanced Programming Lecture 7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Seven:
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Big Java Chapter 7-8 (ArrayList / Class Design) 2009.OOP.r1.
Chapter 11 Arrays Continued
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.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Chapter 18 Java Collections Framework
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
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.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
Lists What to do?. Lists  A list is a linear arrangement of data elements.  Items are arranged in sequential (linear) order  Items are therefore ordered.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
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.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
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];
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
int [] scores = new int [10];
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Principles of Computer Science I
Chapter 8 – Arrays and Array Lists
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
7.6 Common Array Algorithm: Filling
Introduction to Computer Science and Object-Oriented Programming
ARRAYS & ARRAY LISTS.
int [] scores = new int [10];
int [] scores = new int [10];
Presentation transcript:

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[] data = new double[10]; When array is created, all values are initialized depending on array type: –Numbers: 0 –Boolean: false –Object References: null

Array

Java Use [] to access an element data[2] = 29.95;

Arrays Using the value stored: System.out.println("The value of this data item is " + data[4]); Get array length as data.length. (Not a method!) Index values range from 0 to length - 1 Accessing a nonexistent element results in a bounds error double[] data = new double[10]; data[10] = 29.95; // ERROR Limitation: Arrays have fixed length

Array Lists The 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 The ArrayList class is a generic class: ArrayList collects objects of type T: ArrayList accounts = new ArrayList (); accounts.add(new BankAccount(1001)); accounts.add(new BankAccount(1015)); accounts.add(new BankAccount(1022)); size method yields number of elements

Retrieving Array List Elements Use get method Index starts at 0 BankAccount anAccount = accounts.get(2); // gets the third element of the array list Bounds error if index is out of range Most common bounds error: int i = accounts.size(); anAccount = accounts.get(i); // Error // legal index values are 0...i-1

Adding Elements set overwrites an existing value BankAccount anAccount = new BankAccount(1729); accounts.set(2, anAccount); add adds a new value before the index accounts.add(i, a)

Removing Elements remove removes an element at an index accounts.remove(i)

Wrappers You cannot insert primitive types directly into array lists To treat primitive type values as objects, you must use wrapper classes: ArrayList data = new ArrayList (); data.add(29.95); double x = data.get(0);

Wrappers

Auto-boxing Auto-boxing: Starting with Java 5.0, conversion between primitive types and the corresponding wrapper classes is automatic. Double d = 29.95; // auto-boxing; // same as Double d = new Double(29.95); double x = d; // auto-unboxing; //same as double x = d.doubleValue(); Auto-boxing even works inside arithmetic expressions Double e = d + 1; Means: –auto-unbox d into a double –add 1 –auto-box the result into a new Double –store a reference to the newly created wrapper object in e

The Generalized for Loop Traverses all elements of a collection: double[] data =...; double sum = 0; for (double e : data) {//for each e in data sum = sum + e; } Traditional alternative: double[] data =...; double sum = 0; for (int i = 0; i < data.length; i++){ double e = data[i]; sum = sum + e; }

The Generalized for Loop Works for ArrayLists too: ArrayList accounts =... ; double sum = 0; for (BankAccount a : accounts){ sum = sum + a.getBalance(); } Equivalent to the following ordinary for loop: double sum = 0; for (int i = 0; i < accounts.size(); i++){ BankAccount a = accounts.get(i); sum = sum + a.getBalance(); }

Simple Array Algorithms: Counting Matches Check all elements and count the matches until you reach the end of the array list. public class Bank{ public int count(double atLeast){ int matches = 0; for (BankAccount a : accounts){ if (a.getBalance() >= atLeast) matches++; // Found a match } return matches; }... private ArrayList accounts; }

Two-Dimensional Arrays When constructing a two-dimensional array, you specify how many rows and columns you need: final int ROWS = 3; final int COLUMNS = 3; String[][] board = new String[ROWS][COLUMNS]; You access elements with an index pair a[i][j] board[i][j] = "x";

Copying Arrays: Copying Array References Copying an array variable yields a second reference to the same array double[] data = new double[10]; // fill array... double[] prices = data;

Partially Filled Arrays Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming convention: final int DATA_LENGTH = 100; double[] data = new double[DATA_LENGTH]; int dataSize = 0; Update dataSize as array is filled: data[dataSize] = x; dataSize++;

Partially Filled Arrays

Maps and sets HashMap – like an array with Objects (Strings as index HashMap job = new HashMap (); job.put(”John”,”Carpenter”); job.put(”Paul”,”Painter”); System.out.println(job.get(”John”)); if(!job.containsKey(”Mads”)) job.put(”Mads”,”Teacher”);

Sets HashSet – like an array with fast lookup HashSet guests = new HashSet (); guests.add(”John”); guests.add(”Paul”); if(!guests.contains (”Mads”)) job.put(”Sorry, Mads”);

Little Exercise A simple sorting algorithm works on an array by finding the biggest number and put it in swop it with the number in position n-1, The find the biggest number in 0..n-2 and swop it with the number in position n-2. Write the algorithm Fill an array with random numbers Compare running time with the function java.utils.Arrays.sort