Download presentation
Presentation is loading. Please wait.
1
C OMP 110 A RRAYS Instructor: Jason Carter
2
2 O UTLINE for loops Arrays
3
3 S TRINGS = C HAR S EQUENCES JohnF.Kennedy hello 14320 String {sequences of characters}
4
4 S TRING P ROCESSING Prints each character on a separate line int i = 0; while (i < s.length()) { System.out.println(s.charAt(i)); i++; }
5
5 D ISSECTING A L OOP int i = 0; while (i < s.length()) { System.out.println(s.charAt(i)); i++; } Loop Condition Loop Body
6
6 F INGER - GRAINED D ISSECTION // String s declared and initialized earlier int i = 0; while (i < s.length()) { System.out.println(s.charAt(i)); i++; } Loop Condition Loop Body Resetting Loop Variable Initializing Loop Variable for (int i=0; i<s.length(); i++) System.out.println ( s.charAt(i));
7
7 M EANING OF F OR L OOP for ( S1; E; S2) S3 for ( S1; E; S2) S3 for ( ; E; S2) S3 for ( ; E; S2) S3 for ( ; E;) S3 for ( ; E;) S3 for ( ;;) S3 for ( ;;) S3 S1; while (E) { S3; S2; } S1; while (E) { S3; S2; } while (E) { S3; S2; } while (E) { S3; S2; } while (E) { S3; } while (E) { S3; } S1; while ( true ) { S3; S2; } S1; while ( true ) { S3; S2; }
8
8 O THER P REDEFINED T YPES AS S EQUENCES 10098991009080 604050 IntSequence {sequences of integers} 3.83.13.73.13.63.9 DoubleSequence {sequences of doubles} JFKFDRJCBCRRGB StringSequence {sequences of string}
9
9 S EQUENCES OF P ROGRAMMER -D EFINED T YPES loan1loan2loan3 LoanSequence Loan[] temperature1 TemperatureSequence Temperature [] temperature2temperature3 temperature1temperature2 Array Types Arrays Array Element
10
10 O THER S EQUENCES AS A RRAY T YPES 10098991009080 604050 int[] 3.83.13.73.13.63.9 double[] JFKFDRJCBCRRGB String[]
11
11 I NITIALIZING A RRAY D ECLARATION 10098991009080 int[] assignmentScores = {100, 98, 99, 100, 90, 80}; Array Type Array Literal Element Type Array Variable double[] gpas = {3.8, 3.1, 3.7, 3.1, 3.6, 3.9}; 3.83.13.73.13.63.9 String[] initials = {“JFK, “FDR”, “JC”, “BC”, “RR”, “GB”}; JFKFDRJCBCRRGB
12
12 I NITIALIZING A RRAY D ECLARATION S YNTAX [] = {, …, } Loan [] loans = { new ALoan(100000), new AnotherLoan (100)};
13
13 A RRAY T YPES H AVE V ARIABLE S IZE 10098991009080 604050 int[] int[] assignmentScores = {100, 98, 99, 100, 90, 80}; assignmentScores = {60, 40, 50}; 604050 assignmentScores 10098991009080
14
14 A RRAY O PERATIONS String[] initials = {“JFK, “FDR”, “JC”, “BC”, “RR”, “GW”, “WW”}; JFKFDRJCBCRRGWWW initials.length6 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
15
15 U NINITIALIZING A RRAY D ECLARATION int[] assignmentScores; assignmentScores = {60, 40, 50}; 604050 assignmentScores null
16
16 A RRAY E LEMENTS U NINITIALIZED int [] assignmentScores = new int [3]; assignmentScores 000
17
17 O BJECT A RRAY E LEMENTS U NINITIALIZED String[] initials = new String[3]; initials null
18
18 E XAMPLE
19
19 GET S TRINGS () 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
20
20 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
21
21 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
22
22 A NOTHER E XAMPLE
23
23 V ARIABLE -S IZE C OLLECTION filled part unfilled part current size maximum size
24
24 3 James Dean Joe Doe Jane Smith sizearray V ARIABLE -S IZE C OLLECTION filled part unfilled part current size maximum size
25
25 V ARIABLE -S IZE C OLLECTION public class { … final static int A_MAX_SIZE = 50; String[] a = new String[A_MAX_SIZE]; int aSize = 0; … //process a for (int index = 0; index < aSize; index++) System.out.println(a[index]); … final int B_MAX_SIZE = 50; String[] b = new String[B_MAX_SIZE]; int bSize = 0; … //process b … }
26
26 S PECIAL T YPE public class {... AVariableSizeCollection a = new AVariableSizeCollection();... for ( int index = 0; index < a.size; index++) System.out.println(a.contents[index]); … AVariableSizeCollection b = new AVariableSizeCollection();... } public class AVariableSizeCollection { public static final int MAX_SIZE = 50; public String[] contents = new String [MAX_SIZE]; public int size = 0; } No Encapsulation Size Not Updated a.contents[a.size] = Console.readString();
27
27 S UPPORTING E NCAPSULATION public interface …. { public static final int MAX_SIZE = 50; public void addElement(String element); public void print(); } User-specificImplementation-specific
28
28 H ISTORY public interface StringHistory { public void addElement(String element); public int size(); public String elementAt( int index); }
29
29 I MPLEMENTING A H ISTORY public class AStringHistory implements StringHistory { public final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt ( int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { if (isFull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++; }
30
30 U SING A H ISTORY public static void main(String[] args) { StringHistory names = new AStringHistory(); while ( true ) { String input = Console.readString(); if (input.length() > 0) if (input.charAt(0) == 'q') break ; else if (input.charAt(0) == 'p' ) print(names); else names.addElement(input); } Exits from the loop
31
31 P RINTING A H ISTORY static void print(StringHistory strings) { System.out.println("******************"); for ( int elementNum = 0; elementNum < strings.size(); elementNum++) System.out.println(strings.elementAt(elementNum)); System.out.println("******************"); }
32
32 AP OINT H ISTORY Variable-sized Collection History Elements accessed by ObjectEditor Conventions for exporting elements
33
33 C ONVENTIONS FOR V ARIABLE -S IZED C OLLECTION public interface PointHistory { public void addElement ( int x, int y); public Point elementAt ( int index); public int size(); } Read Methods Write Method Arbitrary Type
34
34
35
35 E XTRA S LIDES
36
36 loans null loans null ALoan(10000) AnotherLoan(100) Loan[]Loan
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.