Outline Declaring and Using Arrays Arrays of Objects

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
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 b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Building Java Programs Chapter 7.5
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses on: array declaration and use passing arrays and array.
1 7 Arrays. 2 Arrays are objects that help us organize large amounts of information Chapter 7 focuses on: –array declaration and use –bounds checking.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Arrays Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 7 Arrays. Chapter Scope Array declaration and use Bounds checking Arrays as objects Arrays of objects Command-line arguments Variable-length parameter.
COS 312 DAY 11 Tony Gauvin. Ch 1 -2 Agenda Questions? One Capstone proposal over due – First Progress report due March 5 (next class) Assignment 3 Not.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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:
Chapter 7 Arrays: Part 2. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Outline Declaring and Using Arrays Arrays of Objects Variable Length Parameter.
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 7 Arrays: Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/27 Outline Declaring and Using Arrays Arrays of Objects Variable Length.
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:
Outline Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Variable Length Parameter Lists split()
Arrays.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
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.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Lecture 6 Objects for Organizing Data
Arrays Chapter 7.
COS 312 DAY 14 Tony Gauvin.
Two-Dimensional Arrays
Two-Dimensional Arrays
Data Structures Array - Code.
Each value has a numeric index
Chapter 7: Arrays.
Java How to Program, Late Objects Version, 10/e
Nested Loop Review and Two-Dimensional Arrays
Chapter 8 Multi-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Java Software Solutions Foundations of Program Design Seventh Edition
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Java Software Solutions Foundations of Program Design 8th Edition
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
Data Structures Array - Code.
Chapter 7 Part 2 Edited by JJ Shepherd
Java Software Solutions Foundations of Program Design 9th Edition
Arrays Chapter 7.
Multidimensional Arrays
Multi-Dimensional Arrays
INC 161 , CPE 100 Computer Programming
Arrays in Java.
Multidimensional Arrays Section 6.4
Arrays.
Presentation transcript:

Outline Declaring and Using Arrays Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays Polygons and Polylines Mouse Events and Key Events Copyright © 2012 Pearson Education, Inc.

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 Copyright © 2012 Pearson Education, Inc.

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[][] table = new int[12][50]; A array element is referenced using two index values: value = table[3][6] The array stored in one row can be specified using one index Copyright © 2012 Pearson Education, Inc.

Two-Dimensional Arrays Expression Type Description table int[][] 2D array of integers, or array of integer arrays table[5] int[] array of integers table[5][12] int integer See TwoDArray.java See SodaSurvey.java Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // TwoDArray.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. public class TwoDArray { //----------------------------------------------------------------- // Creates a 2D array of integers, fills it with increasing // integer values, then prints them out. public static void main (String[] args) int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; // Print the table System.out.print (table[row][col] + "\t"); System.out.println(); } Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // TwoDArray.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. public class TwoDArray { //----------------------------------------------------------------- // Creates a 2D array of integers, fills it with increasing // integer values, then prints them out. public static void main (String[] args) int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; // Print the table System.out.print (table[row][col] + "\t"); System.out.println(); } Output 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // SodaSurvey.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. import java.text.DecimalFormat; public class SodaSurvey { //----------------------------------------------------------------- // Determines and prints the average of each row (soda) and each // column (respondent) of the survey scores. public static void main (String[] args) int[][] scores = { {3, 4, 5, 2, 1, 4, 3, 2, 4, 4}, {2, 4, 3, 4, 3, 3, 2, 1, 2, 2}, {3, 5, 4, 5, 5, 3, 2, 5, 5, 5}, {1, 1, 1, 3, 1, 2, 1, 3, 2, 4} }; final int SODAS = scores.length; final int PEOPLE = scores[0].length; int[] sodaSum = new int[SODAS]; int[] personSum = new int[PEOPLE]; continue Copyright © 2012 Pearson Education, Inc.

for (int soda=0; soda < SODAS; soda++) continue for (int soda=0; soda < SODAS; soda++) for (int person=0; person < PEOPLE; person++) { sodaSum[soda] += scores[soda][person]; personSum[person] += scores[soda][person]; } DecimalFormat fmt = new DecimalFormat ("0.#"); System.out.println ("Averages:\n"); System.out.println ("Soda #" + (soda+1) + ": " + fmt.format ((float)sodaSum[soda]/PEOPLE)); System.out.println (); System.out.println ("Person #" + (person+1) + ": " + fmt.format ((float)personSum[person]/SODAS)); Copyright © 2012 Pearson Education, Inc.

Output Averages: Soda #1: 3.2 Soda #2: 2.6 Soda #3: 4.2 Soda #4: 1.9 Person #1: 2.2 Person #2: 3.5 Person #3: 3.2 Person #4: 3.5 Person #5: 2.5 Person #6: 3 Person #7: 2 Person #8: 2.8 Person #9: 3.2 Person #10: 3.8 continue for (int soda=0; soda < SODAS; soda++) for (int person=0; person < PEOPLE; person++) { sodaSum[soda] += scores[soda][person]; personSum[person] += scores[soda][person]; } DecimalFormat fmt = new DecimalFormat ("0.#"); System.out.println ("Averages:\n"); System.out.println ("Soda #" + (soda+1) + ": " + fmt.format ((float)sodaSum[soda]/PEOPLE)); System.out.println (); System.out.println ("Person #" + (person+1) + ": " + fmt.format ((float)personSum[person]/SODAS)); Copyright © 2012 Pearson Education, Inc.

Multidimensional Arrays An array can have many dimensions – if it has more than one dimension, it is called a multidimensional array Each dimension subdivides the previous one into the specified number of elements Each dimension has its own length constant Because each dimension is an array of array references, the arrays within one dimension can be of different lengths these are sometimes called ragged arrays Copyright © 2012 Pearson Education, Inc.