© 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.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
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.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
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.
 Pearson Education, Inc. All rights reserved Arrays.
C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.
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.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Chapter 8: Arrays.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
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.
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.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
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 Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
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.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
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.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
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
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
 2005 Pearson Education, Inc. All rights reserved Arrays.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Introduction to programming in java Lecture 21 Arrays – Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Chapter VII: Arrays.
Arrays Chapter 7.
Arrays 2/4 By Pius Nyaanga.
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.
Chapter 7 The Java Array Object © Rick Mercer.
Arrays Chapter 7.
CISC181 Introduction to Computer Science Dr
Building Java Programs
CSC 142 Arrays [Reading: chapter 12].
Arrays in Java.
Presentation transcript:

© 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 floating-point number an int stores one integer an int stores one integer a reference variable stores a "reference" to an object that may store many other variables a reference variable stores a "reference" to an object that may store many other variables  Java Arrays store a collection of "elements" store a collection of "elements" May be references to objects or may be primitive values May be references to objects or may be primitive values programmers access individual objects with subscript notation [ ] (square brackets) programmers access individual objects with subscript notation [ ] (square brackets) The Java Array Object

Array Construction Array Construction type [] array-reference = new type [ capacity ]; 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]; double[] x = new double[8];  This array stores references to 500 strings String[] name = new String[500]; String[] name = new String[500];  An array named test to store 2,000 integers int capacity = 1000; int capacity = 1000; int[] test = new int[2*capacity]; 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]; BankAccount[] customer = new BankAccount[100];

Accessing Individual Array Elements  Individual array elements are referenced through subscripts of this form: array-name [ int-expression ] array-name [ int-expression ] int-expression is an integer that should be in the range of 0..capacity-1 int-expression is an integer that should be in the range of 0..capacity-1  Examples: x[0] x[0] // Pronounced x sub 0 name[5] name[5] // Pronounced name sub 5 test[99] test[99] // Pronounced test sub 99 customer[12] 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]); 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); assertEquals(5, arrayOfInts.length);

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