© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;

Slides:



Advertisements
Similar presentations
Two Dimensional Arrays and ArrayList
Advertisements

Arrays.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Introduction to Programming with C++ Fourth Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Introduction to Arrays. Useful Array Operations  Finding the Highest Value int [] numbers = new int[50]; int highest = numbers[0]; for (int i = 1; i.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 7: Arrays and the ArrayList Class
© 2012 Pearson Education, Inc. All rights reserved. Lecture 2 (Chapter 8): Arrays and the ArrayList Class Starting Out with Java: From Control Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Chapter 7: Arrays and the ArrayList Class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays and the ArrayList Class Starting Out with Java From Control.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8: Arrays and the ArrayList Class Starting Out with Java: From.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CPS120: Introduction to Computer Science Lecture 15 Arrays.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Arrays.
Arrays.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Chapter 7: Arrays and the ArrayList Class Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
CSI 3125, Preliminaries, page 1 Arrays. CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Two-Dimensional Arrays
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Two-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 8 Slides from GaddisText
Multi-Dimensional Arrays
Dr. Sampath Jayarathna Cal Poly Pomona
Outline Declaring and Using Arrays Arrays of Objects
Multidimensional Arrays Section 6.4
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239; cs239 = new Grade[10]; cs239[0] = new Grade(89.9); count Grade

© 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays of Objects Each element needs to be initialized. for (int i = 0; i < accounts.length; i++) accounts[i] = new BankAccount(); Example: ObjectArray.javaObjectArray.java The accounts variable holds the address of an BankAccount array. Address accounts[1] accounts[0] accounts[3] accounts[2] Address accounts[4] balance: 0.0 balance: 0.0

© 2004 Pearson Addison-Wesley. All rights reserved7-3 Two-Dimensional Arrays A one-dimensional array stores a list of elements A two-dimensional array can be thought of as a table of elements, with rows and columns one dimension two dimensions

© 2004 Pearson Addison-Wesley. All rights reserved7-4 Two-Dimensional Arrays To be precise, in Java a two-dimensional array is an array of arrays A two-dimensional array is declared by specifying the size of each dimension separately: int[][] scores = new int[12][50]; A array element is referenced using two index values: value = scores[3][6] The array stored in one row can be specified using one index

© 2004 Pearson Addison-Wesley. All rights reserved7-5 Two-Dimensional Arrays ExpressionTypeDescription tableint[][] 2D array of integers, or array of integer arrays table[5]int[] array of integers table[5][12]int integer See TwoDArray.java (page 399) TwoDArray.java See SodaSurvey.java (page 400) SodaSurvey.java

© 2004 Pearson Addison-Wesley. All rights reserved7-6 Parallel Arrays names[0] addresses[0] Person #1 names[1] addresses[1] Person #2 names[2] addresses[2] Person #3 names[4] addresses[4] Person #5 names[3] addresses[3] Person #4 Relationship between names and addresses array elements. Parallel arrays are useful when storing data of unlike types. Example: ParallelArrays.javaParallelArrays.java

© 2004 Pearson Addison-Wesley. All rights reserved7-7 Declaring a two-dimensional array requires two sets of brackets and two size declarators  The first one is for the number of rows  The second one is for the number of columns. double[][] scores = new double[3][4]; The two sets of brackets in the data type indicate that the scores variable will reference a two-dimensional array. Notice that each size declarator is enclosed in its own set of brackets. Two-Dimensional Arrays two dimensional array rowscolumns

© 2004 Pearson Addison-Wesley. All rights reserved7-8 Accessing Two-Dimensional Array Elements Accessing one of the elements in a two- dimensional array requires the use of both subscripts. scores[2][1] = 95; 0000 row 0 column 1column 2column 3column 0 row 1 row 2 The scores variable holds the address of a 2D array of doubles. Address

© 2004 Pearson Addison-Wesley. All rights reserved7-9 Initializing a Two-Dimensional Array Initializing a two-dimensional array requires enclosing each row’s initialization list in its own set of braces. int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Java automatically creates the array and fills its elements with the initialization values.  row 0 {1, 2, 3}  row 1 {4, 5, 6}  row 2 {7, 8, 9} Declares an array with three rows and three columns.

© 2004 Pearson Addison-Wesley. All rights reserved7-10 Initializing a Two-Dimensional Array 321 row 0 column 1column 2column 0 row 1 row 2 Address The numbers variable holds the address of a 2D array of int values. int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; produces:

© 2004 Pearson Addison-Wesley. All rights reserved7-11 The length Field To access the length fields of the array: int[][] numbers = { { 1, 2, 3, 4 }, { 5, 6, 7 }, { 9, 10, 11, 12 } }; for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) System.out.println(numbers[row][col]); } Number of rowsNumber of columns in this row. The array can have variable length rows.

© 2004 Pearson Addison-Wesley. All rights reserved7-12 More Than Two Dimensions Java does not limit the number of dimensions that an array may be. More than three dimensions is hard to visualize, but can be useful in some programming problems.