Chapter 4 - Arrays

Chapter 4 - Arrays

1 Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays 4 Processing Array Contents 5 Multiple-Subscripted Arrays 6 ArrayList 2003 Prentice Hall, Inc. All rights reserved. 2 1 Introduction 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 2003 Prentice Hall, Inc. All rights reserved. 3 2 Arrays 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] 2003 Prentice Hall, Inc. All rights reserved. 4

2 Name that this same Arrays of array (Note all elements of array have the name, c) c[0] -45 c[1] 6 c[2] 0 c[3] 72

c[4] 1543 c[5] -89 c[6] 0 c[7] 62 c[8] -3 c[9] 1 c[10] 6453

c[11] 78 Position number of the element within array c 2003 Prentice Hall, Inc. All rights reserved. 5 3 Declaring 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

2003 Prentice Hall, Inc. All rights reserved. 6 3 Declaring 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 [] 2003 Prentice Hall, Inc. All rights reserved. 7 4

Processing Array Contents Array length int [] n = new int [5] size = n.length for(int j = 0; j

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 2003 Prentice Hall, Inc. All rights reserved. 9 4 Processing 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) 2003 Prentice Hall, Inc. All rights reserved. 10 5 Multiple-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 Column 0 a[ 0 ][ 0 ] Column 1

a[ 0 ][ 1 ] Column 2 a[ 0 ][ 2 ] Column 3 a[ 0 ][ 3 ] Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ] Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]

Column subscript Array name Row subscript 2003 Prentice Hall, Inc. All rights reserved. 11 5 Multiple-Subscripted Arrays To initialize Default of 0 Initializers grouped by row in braces int [][] b = { { 1, 2 }, { 3, 4 } }; Row 0 Row 1 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 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 4 12 5 Multiple-Subscripted Arrays Command Line Argument, code 8-19, page 497 2003 Prentice Hall, Inc. All rights reserved. 13 Implement a program to simulate a Hollywood square: 1 2 3 4

5 6 7 8 9 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 Sample program Answer: Yes Complete program 2) Input three positions: 8 1 6 Answer: No 3) Input three positions: 2 5 8 Better Version program Answer: Yes 2003 Prentice Hall, Inc. All rights reserved. 14

Implement a program to simulate a Hollywood square: 8 3 4 1 5 9 6 7 2 Best Version program 2003 Prentice Hall, Inc. All rights reserved. 15 6

Array 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); 2003 Prentice Hall, Inc. All rights reserved.

Recently Viewed Presentations

  • The Safe Foodhandler - Amazon S3

    The Safe Foodhandler - Amazon S3

    the food handler from the operation if you primarily serve a high-risk population. The food handler must be cleared by his or her medical practitioner before returning to work. If the food handler is vomiting or has diarrhea. ... The...
  • www.laboutiquedelpowerpoint.com

    www.laboutiquedelpowerpoint.com

    PRESENTA PRODUCCIONES TANGO CUMPLEAÑOS PRESIDENT CUMPLEAÑOS PRESIDENT * SALIR MARRET Pocas mujeres han inspirado una adoración tan duradera en la conciencia pública como Marilyn Monroe y tienes imágenes inspiradas por este icono en el Centro de Artes Finas de Colorado...
  • Approaches to service delivery and the implications for

    Approaches to service delivery and the implications for

    Approaches to service delivery and the implications for public administration * Dr. Vincent Bagambe (MoH-Global Fund, Uganda) * * * * There is a general trend of decreasing new HIV infections Only Uganda experienced an increase in new HIV infections...
  • Setup Instructions - Monroe Library, Loyola University New ...

    Setup Instructions - Monroe Library, Loyola University New ...

    Register your clicker by tapping the . Add a Clicker . option on your . Profile. screen and entering your 8-character clicker ID. TIP: Use your registered clicker or REEF Polling to respond to questions. Either way, if you have...
  • Day 29 Presentation - High School Math Teachers

    Day 29 Presentation - High School Math Teachers

    INTRODUCTION In this presentation, we are interested in summarizing the contents of day 26, 27 and 28 and examine ourselves how best we understood the topics. We are going to discuss the methods of graphing functions, the inverses of functions...
  • Skill Group Techniques Knife skills Meat, fish and

    Skill Group Techniques Knife skills Meat, fish and

    Make a blended white sauce (starch gelatinisation) to demonstrate understanding of viscosity, liquid/starch ratios, convection/conduction and agitation Make a reduction sauce to demonstrate how evaporation concentrates flavour and changes viscosity Make an emulsion sauce to demonstrate the technical skills of...
  • Year 09 - Mathematics - Unit 11 - Answers

    Year 09 - Mathematics - Unit 11 - Answers

    11.3 - More Compound Measures. 11.4 - Ratios & Proportion. 11.1 - Growth and Decay. Page 670. a.£209.70 . The cost of her train ticket before the increase was £225 ÷ 1.125 = £200, so her train ticket has gone...
  • Introduction to MLA Citations F OR Y O

    Introduction to MLA Citations F OR Y O

    For your Poetry Anthology Project, you will be required to select three published poems and cite those poems in MLA format. Fun Fact: MLA = Modern Language Association. Introduction toMLA Citations. The Basics. ... when citing sources! Learn to use...