Download presentation

Presentation is loading. Please wait.

Published byHailey Hogston Modified over 2 years ago

1
Ch 7 Arrays – The 1 st Data Structure : How Store Data ARRAYS: ARRAYS: Table of same type elements (Objects or Primitives). Table of same type elements (Objects or Primitives). Static - Physical size cannot change (public field length) vs Logical size. Static - Physical size cannot change (public field length) vs Logical size. Access of an array component is through [index] notation. Access of an array component is through [index] notation. Positions in an array are indexed from 0 to length –1. Positions in an array are indexed from 0 to length –1. EXAMPLES: EXAMPLES: int[ ] scores;// scores will reference a block of ints int[ ] scores;// scores will reference a block of ints scores = new int[5];// int[ ] scores = new int[5]; scores = new int[5];// int[ ] scores = new int[5]; double[ ] wages = new double[numEmployees]; double[ ] wages = new double[numEmployees]; BankAcct[ ] accounts = new BankAcct[MAX_ACCTS]; BankAcct[ ] accounts = new BankAcct[MAX_ACCTS]; FILLING AN ARRAY. Literal, Direct, or Assignment. FILLING AN ARRAY. Literal, Direct, or Assignment. Literally Assigning Values: Literally Assigning Values: int[ ] tests = new int[3];//test.length = 3 tests[0] = 90; tests[1] = 84; tests[2] = 93; Direct Assignment : If values are known. Direct Assignment : If values are known. int[ ] primes = {2,3,5,7}; int[ ] primes = {2,3,5,7}; Assignment : Write the code to input scores into an array list until -999 is read. Assume theres a helper method getNum( ) for input. Keep track of the logical size called mySize. Assignment : Write the code to input scores into an array list until -999 is read. Assume theres a helper method getNum( ) for input. Keep track of the logical size called mySize.

2
Code to Fill Array: Code to Fill Array: int mySize = 0; num = getNum( ); while ( num != -999 ) { list[mySize] = num; mySize++; num = getNum( ); } DISPLAYING VALUES: DISPLAYING VALUES: int[ ] list = {5, 3, 2, 9, 1}; int[ ] list = {5, 3, 2, 9, 1}; for( int i = 0; i < list.length ; i++) for( int i = 0; i < list.length ; i++) System.out.print(list[i] + ); System.out.print(list[i] + ); EXAMPLES – Trace the following by hand: EXAMPLES – Trace the following by hand: int[ ] list = new int[5]; int[ ] list = new int[5]; for(int a = 0; a < 5; a++) for(int a = 0; a < 5; a++) list[a] = 2*a;//List contains what? for(int a = 0; a < 5; a++) for(int a = 0; a < 5; a++) list[a] = (1+a) * (1+a) ; //List contains what ?

3
EXAMPLES Cont. – Trace the following by hand: EXAMPLES Cont. – Trace the following by hand: list[0]=1; list[0]=1; for(int a = 1; a < 5; a++) list[a] = 2*list[a-1]; //List contains what? displayArray(list); //TBD displayArray(list); //TBD Random num = new Random( ); Random num = new Random( ); for(int a = 0; a < 5; a++) list[a] = 1+num.nextInt(100); //List contains what? displayArray(list); displayArray(list); ASSUME EXAMPLES ARE INVOKED BY: displayArray(list); ASSUME EXAMPLES ARE INVOKED BY: displayArray(list); public void displayArray(int[ ] arr)// Array as a parameter public void displayArray(int[ ] arr)// Array as a parameter { for( int i = 0; i < arr.length ; i++) System.out.print(arr[i] + ); for( int i = 0; i < arr.length ; i++) System.out.print(arr[i] + ); } MISC: What do you think the following would do? MISC: What do you think the following would do? 1. System.out.print(list[list.length]); 2. System.out.print(list); HOMEWORK HOMEWORK 1. Trace the 4 examples above 2. Run the examples in BlueJ invoking displayArray(list) for each.

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google