Chapter 7 The Java Array Object © Rick Mercer.

Slides:



Advertisements
Similar presentations
Introduction to arrays Data in economy size packages.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Programming with Collections Collections in Java Using Arrays Week 9.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Java Unit 9: Arrays Declaring and Processing Arrays.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Chapter 8: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
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.
8-1 Chapter-7 Part1 Introduction to Arrays –Array Types –Creating/Declaring Arrays –Initializing Arrays –Processing Array Contents –Passing Arrays as Arguments.
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.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
ARRAYS Multidimensional realities Image courtesy of
 2005 Pearson Education, Inc. All rights reserved Arrays.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Sections 10.1 – 10.4 Introduction to Arrays
Arrays Chapter 7.
Two-Dimensional Arrays
Array, Strings and Vectors
C programming---Arrays
Arrays 2/4 By Pius Nyaanga.
Selenium WebDriver Web Test Tool Training
JavaScript: Functions.
Building Java Programs Chapter 7
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Lists in Python.
An Introduction to Java – Part I, language basics
Building Java Programs
Chapter 10 Vectors Computing Fundamentals with C++ 3rd Edition
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays in Java What, why and how Copyright Curt Hill.
Building Java Programs
Defining methods and more arrays
Arrays ICS2O.
Arrays Chapter 7.
Computing Fundamentals with C++
Announcements Lab 6 was due today Lab 7 assigned this Friday
CISC181 Introduction to Computer Science Dr
Building Java Programs
CSC 142 Arrays [Reading: chapter 12].
Array Data Structure Chapter 6
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
ICS103 Programming in C Lecture 12: Arrays I
Presentation transcript:

Chapter 7 The Java Array Object © Rick Mercer

The Java Array Object Some variables store precisely one value: a double stores one floating-point number an int stores one integer a reference variable stores a "reference" to an object that may store many other variables Java Arrays store a collection of "elements" May be references to objects or may be primitive values programmers access individual objects with subscript notation [ ] (square brackets)

Array Construction type [] array-reference = new type[capacity]; type specifies the type of element stored in the array array-name is any valid Java identifier that refers to all elements in the array capacity is an integer expression representing the maximum number of elements that can be stored into the array

Example Array Declarations An array named x that stores up to 8 numbers double[] x = new double[8]; This array stores references to 500 strings String[] name = new String[500]; An array named test to store 2,000 integers int capacity = 1000; int[] test = new int[2*capacity]; An array named customer to stores references to up to 100 unique BankAccount objects BankAccount[] customer = new BankAccount[100];

Accessing Individual Array Elements Individual array elements are referenced through subscripts of this form: array-name[int-expression] int-expression is an integer that should be in the range of 0..capacity-1 Examples: x[0] // Pronounced x sub 0 name[5] // Pronounced name sub 5 test[99] // Pronounced test sub 99 customer[12] // Pronounced customer sub 12

Assignment to individual array elements // All int elements are // set to 0 initially int[] x = new int[5]; x[0] = 1; x[1] = 2 x[2] = 5; x[3] = x[0] + x[2]; x[4] = x[3] - 1; for(int j = 0; j < x.length; j++) { // What is the Output? System.out.println(x[j]); }

Out of Range Subscripts Subscripts can be "out of range” String[] name = new String[1000]; name[-1] = "Subscript too low"; name[ 0] = "This should be the first name"; name[999] = "This is the last good subscript"; name[1000] = "Subscript too high"; Two of the above references will cause ArrayIndexOutOfBounds exceptions and "ungracefully" terminate the program with: java.lang.ArrayIndexOutOfBounds exception: -1

Array initializer (a shortcut) and the length variable Arrays can be initialized with an array initializer int[] arrayOfInts = { 55, 33, 77, 22, -99 }; assertEquals(55, arrayOfInts[0]); assertEquals(33, arrayOfInts[1]); assertEquals(77, arrayOfInts[2]); assertEquals(22, arrayOfInts[3]); assertEquals(-99, arrayOfInts[4]); // arrayReference.length returns capacity assertEquals(5, arrayOfInts.length);

Let's complete this method in a JUnit test Passing Array Arguments Imagine a method that returns true if the first and last values are equal double[] a1 = { 5.0, 4.0, 3.0, 2.0 }; assertFalse(endsSame(a1)); double[] a2 = { 5.0, 4.0, 3.0, 2.0, -1.0, 5.0 }; assertTrue(endsSame(a2)); The method heading has an array parameter public boolean endsSame(double[] x) { } Let's complete this method in a JUnit test

Process all elements with a for loop Imagine a method that returns the average of the array elements double[] x1 = { 5.0, 4.0, 3.0, 2.0 }; assertEquals(3.5, average(x1), 0.001); double[] x2 = { 5.0, 4.0, 3.0, 2.0, -1.0, 5.0 }; assertEquals(3.0, average(x2), 0.001); This method will use an array parameter public double average(double[] x) { } Let's complete this method in a JUnit test

Answers public boolean endsSame(double[] x) { return x[0] == x[x.length - 1]; } public double average(double[] x) { int n = x.length; double total = 0; // get the sum of all elements for (int i = 0; i < n; i++) { total += x[i]; // Compute and return the average return total / n;