Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.

Similar presentations


Presentation on theme: " 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted."— Presentation transcript:

1  2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted Arrays 6 ArrayList

2  2003 Prentice Hall, Inc. All rights reserved. 2 1Introduction Array –Consecutive group of values (i.e., 3x3 caption value of buttons in q6 in project 3) –Same name and type ( int, char, etc.) To refer to an element –Specify array name and position number (index) –Format: arrayname[ position number ] –First element at position 0 (off-by-one error, p442) N-element array c (Figure 8-2, page 436) c[ 0 ], c[ 1 ] … c[ n - 1 ] –Nth element as position N-1

3  2003 Prentice Hall, Inc. All rights reserved. 3 2Arrays Array elements like other variables –Figure 8-4, page 438 (Concept) Assignment, printing for an integer array c c[ 0 ] = 3; System.out.print (c[ 0 ]); –Code 8-1, page 438 (access of any element) –Code 8-2, page 440 (loop based access) Can perform operations inside subscript c[ 5 – 2 ] same as c[3]

4  2003 Prentice Hall, Inc. All rights reserved. 4 2Arrays c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c

5  2003 Prentice Hall, Inc. All rights reserved. 5 3Declaring Arrays When declaring arrays, specify –Name –Type of array Any data type –Number of elements –type [] arrayName = new type [ arraySize ]; int [] c = new int [ 10 ]; // array of 10 integers float [] d = new float [ 3284 ]; // array of 3284 floats

6  2003 Prentice Hall, Inc. All rights reserved. 6 3Declaring Arrays Initializing arrays (p443) –For loop Set each element –Initializer list Specify each element when array declared int [] n = { 1, 2, 3, 4, 5 }; –If array size omitted, initializers determine size –int [] n = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array Alternative declaration (p444) –int [] n –int n []

7  2003 Prentice Hall, Inc. All rights reserved. 7 4Processing Array Contents Array length –int [] n = new int [5] size = n.length for(int j = 0; j<size; j++) –Enhanced for loop for (dataType elementVal : array) statement; for (int x: n) System.out.println(x); Or for (dataType elementVal : array) { statement1; statement2; } for (int x: n) { System.out.print(“The next value is ”); System.out.println(x); }

8  2003 Prentice Hall, Inc. All rights reserved. 8 4Processing Array Contents Dynamic data storage (user defined size of array) –Code 8-6, page 450 –int [] n; n= new int [n_size]; Copy –Code 8-7, page 453 Pass array in argument –Bring back all the change on array, multiple return –Code 8-9, page 456 –Code 8-12, page 468

9  2003 Prentice Hall, Inc. All rights reserved. 9 4Processing Array Contents Summing the values in an array Finding the highest and lowest value in an array String array, code 8-13, page 470 Search –Unsorted array: linear search –Sorted array: binary search (Code 8-23, page 505) Array of objects (such as buttons)

10  2003 Prentice Hall, Inc. All rights reserved. 10 5Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –dType [][] aName = new dType [n_row][n_column] –Tables with rows and columns –Specify row, then column –“Array of arrays” a[0] is an array of 4 elements a[0][0] is the first element of that array Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript

11  2003 Prentice Hall, Inc. All rights reserved. 11 5Multiple-Subscripted Arrays To initialize –Default of 0 –Initializers grouped by row in braces int [][] b = { { 1, 2 }, { 3, 4 } }; Length, code 8-17, page 484 Ragged array (dynamic storage must) –int [] [] n = new int [4] [] n[2]=new int [5] n[3]=new int [6] –Page 489 1 2 3 4 Row 0Row 1

12  2003 Prentice Hall, Inc. All rights reserved. 12 5Multiple-Subscripted Arrays Command Line Argument, code 8-19, page 497

13  2003 Prentice Hall, Inc. All rights reserved. 13 Implement a program to simulate a Hollywood square: Input three different numbers, to check if they are in the same column, row or diagonal. For example: 1) Input three positions: 1 2 3 Answer: Yes 2) Input three positions: 8 1 6 Answer: No 3) Input three positions: 2 5 8 Answer: Yes Sample program Complete program Better Version program

14  2003 Prentice Hall, Inc. All rights reserved. 14 Implement a program to simulate a Hollywood square: Best Version program

15  2003 Prentice Hall, Inc. All rights reserved. 15 6Array List Page 525-536 ArrayList nameList = new ArrayList ( ); Import java.util.ArrayList; nameList.add( content ); nameList.size( ); nameList.get(index); //casting nameList.remove(index); nameList.add(index, content); nameList.set(index, new_content);


Download ppt " 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted."

Similar presentations


Ads by Google