Java As An Object-Oriented Programming Language: Arrays
Java As An Object-Oriented Programming Language: Arrays
Java as an Object-oriented
Programming Language
Arrays
Array basics
2
Data Structures
index 0 1 2 3 4 5 6 7 8 9
value 12 49 -2 26 5 17 -6 84 72 3
5
Array declaration
Declaring/initializing an array:
<type>[] <name> = new <type>[<length>];
Example:
int[] numbers = new int[10];
index 0 1 2 3 4 5 6 7 8 9
value 0 0 0 0 0 0 0 0 0 0
7
Array auto-initialization: Example
An array of doubles
index 0 1 2 3 4
value 0.0 0.0 0.0 0.0 0.0
An array of booleans
index 0 1 2 3
value false false false false
8
Don't go out of bounds!
Example:
int[] data = new int[10];
System.out.println(data[0]); // okay
System.out.println(data[-1]); //
exception!
System.out.println(data[9]); // okay
System.out.println(data[10]); //
exception!
index 0 1 2 3 4 5 6 7 8 9
value 0 0 0 0 0 0 0 0 0 9
0
The length field
General syntax:
<array name>.length
10
Array initialization statement
Example:
int[] numbers = { 12, 49, -2, 26, 5, 17, -6 };
index 0 1 2 3 4 5 6
value 12 49 -2 26 5 17 -6
Example:
int[] a = { 2, 5, 1, 6, 14, 7, 9 };
for (int i = 1; i < a.length; i++) {
a[i] += a[i - 1];
}
System.out.println("a is " + Arrays.toString(a));
Output:
a is [2, 7, 8, 14, 28, 35, 44]
12
Enum
Example:
public enum Monster{ZOMBIE, VAMPIRE, DEMON,
WEREWOLF};
15
How can we store a table of values?
...
student8: 4.5 2 9 9 5.5 4 7.5 6 5 9
Tedious!
17
Ragged 2-D arrays
quizScores
quizScores[0]
quizScores[1]
quizScores[2]
quizScores[3]
Text processing
19
Text processing
21
Strings
// and assign
String Methods
23
The charAt method
24
Calling string methods
Example:
// print the alphabet
for (char c = 'a'; c <= 'z'; c++) {
System.out.print(c);
}
26
char vs. String
'h' is a char
char c = 'h';
char values are primitive; you cannot call methods on them
can't say c.length() or c.toUpperCase()
"h" is a String
String s = "h";
Strings are objects; they contain methods that can be called
can say s.length() 1
can say s.toUpperCase() "H"
can say s.charAt(0) 'h'
27
Comparing strings
Example:
Scanner console = new Scanner(System.in);
System.out.print("What is your name? ");
String name = console.next();
if (name.equals("Barney")) {
System.out.println("I love you, you love
me,");
System.out.println("We're a happy family!");
}
28
Comparing strings
There are more methods of a String object that can be
used in <test> conditions.
Method Description
equals(str) whether this string contains exactly the
same characters as the other string
equalsIgnoreCase(str) whether this string contains the same
characters as the other, ignoring upper-
vs. lowercase differences
startsWith(str) whether this string’s beginning matches
the argument
endsWith(str) whether this string’s end matches the
argument
29
Comparing strings: Examples
if (fullName.startsWith("Giorgio")) {
System.out.println("When are you retiring?");
}
if (lastName.equalsIgnoreCase("lumBerg")) {
System.out.println("I need your TPS reports!");
}
if (name.toLowerCase().indexOf("sr.") >= 0) {
System.out.println("You must be old!");
}
30
What about String typecasts?
import java.util.Scanner;
35
StringBuilder Constructors
java.lang.StringBuilder
36
Modifying Strings in the Builder
java.lang.StringBuilder
+append(data: char[]): StringBuilder Appends a char array into this string builder.
+append(data: char[], offset: int, len: int): Appends a subarray in data into this string builder.
StringBuilder
+append(v: aPrimitiveType): StringBuilder Appends a primitive type value as a string to this
builder.
+append(s: String): StringBuilder Appends a string to this string builder.
+delete(startIndex: int, endIndex: int): Deletes characters from startIndex to endIndex.
StringBuilder
+deleteCharAt(index: int): StringBuilder Deletes a character at the specified index.
+insert(index: int, data: char[], offset: int, Inserts a subarray of the data in the array to the builder
len: int): StringBuilder at the specified index.
+insert(offset: int, data: char[]): Inserts data into this builder at the position offset.
StringBuilder
+insert(offset: int, b: aPrimitiveType): Inserts a value converted to a string into this builder.
StringBuilder
+insert(offset: int, s: String): StringBuilder Inserts a string into this builder at the position offset.
+replace(startIndex: int, endIndex: int, s: Replaces the characters in this builder from startIndex
String): StringBuilder to endIndex with the specified string.
+reverse(): StringBuilder Reverses the characters in the builder.
+setCharAt(index: int, ch: char): void Sets a new character at the specified index in this
37
builder.
The toString, capacity, length, setLength, and charAt
Methods
java.lang.StringBuilder
38
Examples
stringBuilder.append("Java");
stringBuilder.insert(11, "HTML and ");
stringBuilder.delete(8, 11) changes the builder to Welcome Java.
stringBuilder.deleteCharAt(8) changes the builder to Welcome o Java.
stringBuilder.reverse() changes the builder to avaJ ot emocleW.
stringBuilder.replace(11, 15, "HTML")
changes the builder to Welcome to HTML.
stringBuilder.setCharAt(0, 'w') sets the builder to welcome to Java.
39
StringTokenizer
boolean hasMoreTokens()
String nextToken()
String nextToken(String delim)
StringTokenizer
+countTokens(): int
+hasMoreTokens():boolean
+nextToken(): String
+nextToken(delim: String): String
14/10/2019 40