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];

Slides:



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

Two Dimensional Arrays and ArrayList
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.
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.
Arrays.
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,
Programming with Collections Collections in Java Using Arrays Week 9.
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.
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.
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]
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional 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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
ICOM 4015: Advanced Programming Lecture 7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Seven:
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Arrays.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Chapter 7 – Arrays and Array Lists
Principles of Computer Science I
Chapter 8 – Arrays and Array Lists
[ 4.00 ] [ Today’s Date ] [ Instructor Name ]
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Introduction to Computer Science and Object-Oriented Programming
Object Oriented Programming in java
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays.
Presentation transcript:

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];

Arrays When array is created, all values are initialized depending on array type: – Numbers: 0 – Boolean: false – Object References: null

Arrays Figure 1: An Array Reference and an Array

Arrays Use [ ] to access an element Figure 2: Storing a Value in an Array data[2] = 29.95;

Arrays Using the value stored: Get array length as data.length. (Not a method!) Index values range from 0 to length - 1 System.out.println("The value of this data item is " + data[4]); Continued…

Arrays Accessing a nonexistent element results in a bounds error Limitation: Arrays have fixed length double[] data = new double[10]; data[10] = 29.95; // ERROR

Syntax 8.1: Array Construction new typeName[length] Example: new double[10] Purpose: To construct an array with a given number of elements

Syntax 8.2: Array Element Access arrayReference[index] Example: data[2] Purpose: To access an element in an array

The Generalized for Loop Traverses all elements of a collection: Continued… double[] data =...; double sum = 0; for (double e : data) // You should read this loop as "for each e in data" { sum = sum + e; }

The Generalized for Loop Traditional alternative: double[] data =...; double sum = 0; for (int i = 0; i < data.length; i++) { double e = data[i]; sum = sum + e; }

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

A Tic-Tac-Toe Board Figure 6: A Tic-Tac-Toe Board

Traversing Two-Dimensional Arrays It is common to use two nested loops when filling or searching: for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLUMNS; j++) board[i][j] = " ";

Array Coding Problems Page 367 E7.6 Alternating Sums Page 367 E7.10 Sequence Checker Finish Alternating Sums and Sequence Checker in class today! Page 368 E7.15 Magic Squares Magic Squares takes some time – must put upfront design thought into it before coding. Design (pseudocode) due Thursday

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 Continued…

Array Lists The ArrayList class is a generic class: ArrayList collects objects of type T : size method yields number of elements ArrayList accounts = new ArrayList (); accounts.add(new BankAccount(1001)); accounts.add(new BankAccount(1015)); accounts.add(new BankAccount(1022));

Retrieving Array List Elements Use get method Index starts at 0 Bounds error if index is out of range BankAccount anAccount = accounts.get(2); // gets the third element of the array list Continued…

Retrieving Array List Elements 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 add adds a new value before the index Continued… BankAccount anAccount = new BankAccount(1729); accounts.set(2, anAccount); accounts.add(i, a)

Adding Elements Figure 3: Adding an Element in the Middle of an Array List

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

Removing Elements Figure 4: Removing an Element in the Middle of an Array List

Self Check 3.How do you construct an array of 10 strings? An array list of strings? 4.What is the content of names after the following statements? ArrayList names = new ArrayList (); names.add("A"); names.add(0, "B"); names.add("C"); names.remove(1);

Answers names contains the strings " B " and " C " at positions 0 and 1 new String[10]; new ArrayList ();