 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.

Slides:



Advertisements
Similar presentations
PAC Introduction to Methods Professor: Evan Korth New York University.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Arrays Chapter 6.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Computer Science I Arrays Professor: Evan Korth New York University.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 Pearson Education, Inc. All rights reserved Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference Parameters.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays. Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Review for Nested loops & Math class methods & User defined methods.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Arrays Chapter 7.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
7 Arrays.
7 Arrays.
7 Arrays.
Object Oriented Programming in java
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in JAVA: V

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Arrays –Data structures which reference one or more values –All items must have the same data type –Remain the same size once they are created Fixed-length entries

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Fig. 7.1A 12-element array. Name of array (note that all elements of this array have the same name, c ) Index (or subscript) of the element in array c c[ 0 ] c[ 1 ] c[ 2 ] c[ 3 ] c[ 4 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] c[ 10 ] c[ 11 ]

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Array vs. variable Variables hold one value of one data type. –Num = 70; Arrays reference multiple elements of the same data type. –Num[0]= 70; –Num[1]=20; –Now, it becomes easier to compare, search and/or sort all of the elements of the array since we have access to all of the them at the same time.

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Arrays (cont.) Index –Also called subscript –Position number in square brackets –Must be positive integer or integer expression – Assign a value to array element 1 c[ 0 ] = 2; Adds 2 to six element: c[ 5 ] C[5] +=2; Index = 0 Reference first element of the array Index= 5 reference array element 6

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Arrays (cont.) Examine array c –c is the array name –c.length accesses array c ’s length –c has 12 elements ( c[0], c[1], … c[11] )

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Declaring and Creating Arrays Declaring and Creating arrays –Arrays are objects that occupy memory –Created dynamically with the keyword new –Or you can declare and create in two lines: int c[ ]; // declare array c = new int[ 12 ]; // creates an array of type int –Creating an array and declaring it in one line: int c[ ] = new int[ 12 ]; –We can create arrays of objects with String data type String b[ ] = new String[ 100 ];

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Examples Using Arrays Declaring arrays Creating arrays Initializing arrays Manipulating array elements

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Examples Using Arrays (Cont.) Creating and initializing an array –Declare array –Create array –Initialize array elements

 2003 Prentice Hall, Inc. All rights reserved. InitArray.java Line 9 Declare array as an array of int s Line 11 Create 10 int s for array ; each int is initialized to 0 by default Line 16 array.length returns length of array Line 17 array[counter] returns int associated with index in array 1 // Fig. 7.2: InitArray.java 2 // Creating an array. 3 import javax.swing.*; 4 5 public class InitArray { 6 7 public static void main( String args[] ) 8 { 9 int array[]; // declare reference to an array array = new int[ 10 ]; // create array String output = "Index\tValue\n"; // append each array element's value to String output 16 for ( int counter = 0; counter < array.length; counter++ ) 17 output += counter + "\t" + array[ counter ] + "\n"; JOptionPane.showMessageDialog( null, output, 21 "Initializing an Array of int Values", 22 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class InitArray Declare array as an array of int s Create 10 int s for array ; each int is initialized to 0 by default array.length returns length of array array[counter] returns int associated with index in array

 2003 Prentice Hall, Inc. All rights reserved. InitArray.java Each int is initialized to 0 by default Each int is initialized to 0 by default Examples Using Arrays (Cont.)

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Examples Using Arrays (Cont.) Using an array initializer –Use initializer list Items are enclosed in braces ( {} ) Items within the list are separated by commas int n[ ] = { 10, 20, 30, 40, 50 }; –Creates a five-element array –Index values of 0, 1, 2, 3, 4 –Note: in this case, we do not need the keyword new

 2003 Prentice Hall, Inc. All rights reserved. InitArray.java Line 11 Declare array as an array of int s Line 11 Compiler uses initializer list to allocate array 1 // Fig. 7.3: InitArray.java 2 // Initializing an array with a declaration. 3 import javax.swing.*; 4 5 public class InitArray { 6 7 public static void main( String args[] ) 8 { 9 // array initializer specifies number of elements and 10 // value for each element 11 int array[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; String output = "Index\tValue\n"; // append each array element's value to String output 16 for ( int counter = 0; counter < array.length; counter++ ) 17 output += counter + "\t" + array[ counter ] + "\n"; JOptionPane.showMessageDialog( null, output, 23 "Initializing an Array with a Declaration", 24 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class InitArray Declare array as an array of int s Compiler uses initializer list to allocate array

 2003 Prentice Hall, Inc. All rights reserved. InitArray.java Each array element corresponds to element in initializer list Each array element corresponds to element in initializer list

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Examples Using Arrays (Cont.) Calculating the value to store in each array element –Initialize elements of 10-element array to even integers

 2003 Prentice Hall, Inc. All rights reserved. InitArray.java Line 10 Declare array as an array of int s Line 12 Create 10 int s for array Line 16 Use array index to assign array value 1 // Fig. 7.4: InitArray.java 2 // Initialize array with the even integers from 2 to import javax.swing.*; 4 5 public class InitArray { 6 7 public static void main( String args[] ) 8 { 9 final int ARRAY_LENGTH = 10; // constant 10 int array[]; // reference to int array array = new int[ ARRAY_LENGTH ]; // create array // calculate value for each array element 15 for ( int counter = 0; counter < array.length; counter++ ) 16 array[ counter ] = * counter; String output = "Index\tValue\n"; for ( int counter = 0; counter < array.length; counter++ ) 21 output += counter + "\t" + array[ counter ] + "\n"; System.out.print ( output ); 25 Declare array as an array of int s Create 10 int s for array Use array index to assign array value

 2003 Prentice Hall, Inc. All rights reserved. InitArray.java 30 System.exit( 0 ); } // end main } // end class InitArray

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Examples Using Arrays (Cont.) Summing the elements of an array –Array elements can represent a series of values We can sum these values

 2003 Prentice Hall, Inc. All rights reserved. SumArray.java Line 9 Declare array with initializer list Lines Sum all array values 1 // Fig. 7.5: SumArray.java 2 // Total the values of the elements of an array. 3 import javax.swing.*; 4 5 public class SumArray { 6 7 public static void main( String args[] ) 8 { 9 int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 10 int total = 0; // add each element's value to total 13 for ( int counter = 0; counter < array.length; counter++ ) 14 total += array[ counter ]; JOptionPane.showMessageDialog( null, 17 "Total of array elements: " + total, 18 "Sum the Elements of an Array", 19 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class SumArray Declare array with initializer list Sum all array values

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Examples Using Arrays (Cont.) Using histograms do display array data graphically –Histogram Plots each numeric value as bar of asterisks ( * )

 2003 Prentice Hall, Inc. All rights reserved. Histogram.java Line 9 Declare array with initializer list Line 19 For each array element, print associated number of asterisks 1 // Fig. 7.6: Histogram.java 2 // Histogram printing program. 3 import javax.swing.*; 4 5 public class Histogram { 6 7 public static void main( String args[] ) 8 { 9 int array[] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; String output = "Element\tValue\tHistogram"; // for each array element, output a bar in histogram 14 for ( int counter = 0; counter < array.length; counter++ ) { 15 output += "\n" + counter + "\t" + array[ counter ] + "\t"; // print bar of asterisks 18 for ( int stars = 0; stars < array[ counter ]; stars++ ) 19 output += "*"; } // end outer for Declare array with initializer list For each array element, print associated number of asterisks

 2003 Prentice Hall, Inc. All rights reserved. Histogram.java 26 JOptionPane.showMessageDialog( null, output, 27 "Histogram Printing Program", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class Histogram

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Using Arrays (Cont.) Using the elements of an array as counters –Use a series of counter variables to summarize data

 2003 Prentice Hall, Inc. All rights reserved. RollDie.java Line 9 Declare frequency as array of 7 int s Lines Generate 6000 random integers in range 1-6 Line 13 Increment frequency values at index associated with random number 1 // Fig. 7.7: RollDie.java 2 // Roll a six-sided die 6000 times. 3 import javax.swing.*; 4 5 public class RollDie { 6 7 public static void main( String args[] ) 8 { 9 int frequency[] = new int[ 7 ]; // roll die 6000 times; use die value as frequency index 12 for ( int roll = 1; roll <= 6000; roll++ ) 13 ++frequency[ 1 + ( int ) ( Math.random() * 6 ) ]; String output = "Face\tFrequency"; // append frequencies to String output 18 for ( int face = 1; face < frequency.length; face++ ) 19 output += "\n" + face + "\t" + frequency[ face ]; JOptionPane.showMessageDialog( null, output, 25 "Rolling a Die 6000 Times", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class RollDie Declare frequency as array of 7 int s Generate 6000 random integers in range 1-6 Increment frequency values at index associated with random number

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Array review Figuring out the array positions In Java, an array’s elements start at index 0 and go up to (the number of elements – 1) For example, our array of 10 student grades filled in with grades might look like: Value Index

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Keyword new array - review The keyword new is used in Java when you wish to create a new object. In Java, arrays are objects. We will not speak about the details of an object. All you need to know is in the case of arrays, new is used to create the new array. All positions of the new array will automatically be initialized to the default value for the array’s type.

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Some powerful features of arrays Java can use expressions as the subscript –For example, if variables a = 1 and b = 2 studentGrades[ a + b ] would be the same as writing … studentGrades[ 3 ] Java can use array elements in expressions ++ studentGrades[1]; … increments the value of array element #2 by one. This is the same as: studentGrades[1] = studentGrades[1] + 1;

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Using Arrays (Cont.) Using arrays to analyze survey results –40 students rate the quality of food 1-10 Rating scale: 1 mean awful, 10 means excellent –Place 40 responses in array of integers –Summarize results

 2003 Prentice Hall, Inc. All rights reserved. StudentPoll.jav a Lines 9-11 Declare responses as array to store 40 responses Line 12 Declare frequency as array of 11 int and ignore the first element Lines For each response, increment frequency values at index associated with that response 1 // Fig. 7.8: StudentPoll.java 2 // Student poll program. 3 import javax.swing.*; 4 5 public class StudentPoll { 6 7 public static void main( String args[] ) 8 { 9 int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 11 4, 8, 6, 8, 10 }; 12 int frequency[] = new int[ 11 ]; // for each answer, select responses element and use that value 15 // as frequency index to determine element to increment 16 for ( int answer = 0; answer < responses.length; answer++ ) 17 ++frequency[ responses[ answer ] ]; String output = "Rating\tFrequency\n"; // append frequencies to String output 22 for ( int rating = 1; rating < frequency.length; rating++ ) 23 output += rating + "\t" + frequency[ rating ] + "\n"; Declare responses as array to store 40 responses Declare frequency as array of 11 int and ignore the first element For each response, increment frequency values at index associated with that response

 2003 Prentice Hall, Inc. All rights reserved. StudentPoll.jav a 28 JOptionPane.showMessageDialog( null, output, 29 "Student Poll Program", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class StudentPoll

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Examples Using Arrays (Cont.) Some additional information –When looping through an array The index should never go below 0 The index should be less than the total number of array elements –When invalid array reference occurs Java generates ArrayIndexOutOfBoundsException –Chapter 15 discusses exception handling

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. References and Reference Parameters Two ways to pass parameters to methods –Pass-by-value Copy of the parameter’s value is passed to the called method In Java, every primitive variable is handled with pass-by-value –Pass-by-reference Caller method gives the called method direct access to the caller’s data The called method can then manipulate these data. This provides an improved performance over pass-by-value. In Java, every object is used with pass-by-reference –In Java, arrays are objects Therefore, arrays are passed to methods by reference

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Passing Arrays to Methods To pass an array parameter to a method –Specify the array name without brackets Array hourlyTemperatures is declared as int hourlyTemperatures = new int[ 24 ]; The method call is written as folows: modifyArray( hourlyTemperatures ); This passes the array hourlyTemperatures to the method called modifyArray

 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Passing arrays to methods (cont) In the method header, use similar syntax as that for array declaration: public static void passArray(int array [] ) or public static void passArray(int [] array )