Comp 110 For and Arrays

20
COMP 110 FOR AND ARRAYS Instructor: Prasun Dewan

description

Comp 110 For and Arrays. Instructor: Prasun Dewan. Prerequisite. Loops Advanced. Strings = Char Sequences. String {sequences of characters}. J. o. h. n. F. K. e. n. n. e. d. y. h. e. l. l. o. 1. 4. 3. 2. 0. Other Predefined Types as Sequences. - PowerPoint PPT Presentation

Transcript of Comp 110 For and Arrays

Page 1: Comp 110 For and Arrays

COMP 110FOR AND ARRAYS

Instructor: Prasun Dewan

Page 2: Comp 110 For and Arrays

2

PREREQUISITE

Loops Advanced

Page 3: Comp 110 For and Arrays

3

STRINGS = CHAR SEQUENCES

J o h n F . K e n n e d y

h e l l o

1 4 3 2 0

String {sequences of characters}

Page 4: Comp 110 For and Arrays

4

OTHER PREDEFINED TYPES AS SEQUENCES

100

98 99100

90 80

60 40 50

IntSequence {sequences of integers}

3.8 3.1 3.7 3.1 3.6 3.9

DoubleSequence {sequences of doubles}

JFK FDR JC BC RR GB

StringSequence {sequences of string}

Page 5: Comp 110 For and Arrays

5

SEQUENCES OF PROGRAMMER-DEFINED TYPES

loan1 loan2 loan3

LoanSequenceLoan[]

temperature1

TemperatureSequenceTemperature []

temperature2

temperature3

temperature1

temperature2

Array Types

Arrays

Array Element

Page 6: Comp 110 For and Arrays

6

OTHER SEQUENCES AS ARRAY TYPES

100

98 99100

90 80

60 40 50

int[]

3.8 3.1 3.7 3.1 3.6 3.9

double[]

JFK FDR JC BC RR GB

String[]

Page 7: Comp 110 For and Arrays

7

INITIALIZING ARRAY DECLARATION

100

98 99100

90 80

int[] assignmentScores = {100, 98, 99, 100, 90, 80};

Array Type

Array Literal

ElementType

Array Variable

double[] gpas = {3.8, 3.1, 3.7, 3.1, 3.6, 3.9};

3.8 3.1 3.7 3.1 3.6 3.9

String[] initials = {“JFK, “FDR”, “JC”, “BC”, “RR”, “GB”};

JFK FDR JC BC RR GB

Page 8: Comp 110 For and Arrays

8

INITIALIZING ARRAY DECLARATION SYNTAX

<ElementType> [] <arrayVariable> = {<element1>, …, <elementN>}

Loan [] loans = {new ALoan(100000), new AnotherLoan (100)};

Page 9: Comp 110 For and Arrays

9

ARRAY TYPES HAVE VARIABLE SIZE

100

98 99100

90 80

60 40 50

int[]

int[] assignmentScores = {100, 98, 99, 100, 90, 80};

assignmentScores = new int[] {60, 40, 50};

60 40 50

assignmentScores

100

98 99100

90 80

Page 10: Comp 110 For and Arrays

10

ARRAY OPERATIONSString[] initials = {“JFK, “FDR”, “JC”, “BC”, “RR”, “GW”,

“WW”};

JFK FDR JC BC RR GW WW

initials.length 6

public named constant

initials[0] JFK

initials[initials.length-1] WW

initials[initials.length] ArrayIndexOutOfBounds Exception

initials[0] = “HT”

initials[initials.length] = “HT”

ArrayIndexOutOfBounds Exception

Array instance size fixed!

HT

Page 11: Comp 110 For and Arrays

11

UNINITIALIZING ARRAY DECLARATION

int[] assignmentScores;

assignmentScores = {60, 40, 50};

60 40 50

assignmentScores

null

Page 12: Comp 110 For and Arrays

12

ARRAY ELEMENTS UNINITIALIZED

int[] assignmentScores = new int[3];

assignmentScores

0 0 0

Page 13: Comp 110 For and Arrays

13

OBJECT ARRAY ELEMENTS UNINITIALIZED

String[] initials = new String[3];

initials

null null null

Page 14: Comp 110 For and Arrays

14

EXAMPLE

Page 15: Comp 110 For and Arrays

15

GETSTRINGS()

static String[] getStrings() { System.out.println("Number of Strings:"); int numElements = Console.readInt(); System.out.println("Please enter " + numElements + " strings"); String[] strings = new String[numElements]; for (int elementNum = 0; elementNum < numElements; elementNum++) strings[elementNum] = Console.readString(); return strings;}

Variable

Page 16: Comp 110 For and Arrays

16

PRINT()

static void print(String[] strings) { System.out.println("******************"); for ( int elementNum = 0; elementNum < strings.length; elementNum++) System.out.println(strings[elementNum]); System.out.println("******************");}

String array of arbitrary dimension

Page 17: Comp 110 For and Arrays

17

MAIN()

public static void main(String[] args){ String[] names = getStrings(); String command = Console.readString(); while (command.length() > 0 && command.charAt(0) != ‘q’) { if (command.charAt(0) == 'p')

print(names); command = Console.readString(); }}

Must test that length is at least 1 before accessing

char at position 0

No need to test length here

Page 18: Comp 110 For and Arrays

18

Page 19: Comp 110 For and Arrays

19

EXTRA SLIDES

Page 20: Comp 110 For and Arrays

20

loans null

loans

null

null

ALoan(10000)

AnotherLoan(100)

Loan[] Loan