Strings and Arrays: Hendrik Speleers
Strings and Arrays: Hendrik Speleers
Strings and Arrays: Hendrik Speleers
Hendrik Speleers
Strings and Arrays
●
Overview
– Characters and strings
●
String manipulation
●
Formatting output
– Arrays
●
One-dimensional
●
Two-dimensional
– Container classes
●
List: ArrayList and LinkedList
●
Iterating over a list
NMCGJ
2020-2021
Strings and Arrays
●
Characters and strings
– A character (char primitive)
●
Based on Unicode
●
Indicated by single quotation
char c = 'a';
String str = "UNICODE for a is " + (int) c;
if (c < 'W') {...}
NMCGJ
2020-2021
Strings and Arrays
●
Characters and strings
– Special characters: using an escape sequence (with backslash)
Code Meaning
\t tab
\b backspace
\n newline
\r carriage return
\' single quote
\" double quote
\\ backslash
NMCGJ
2020-2021
Strings and Arrays
●
Characters and strings
– Some useful String methods
NMCGJ
2020-2021
Strings and Arrays
●
Java program: string manipulation
– Task
●
Counting the number of words in a string
– Algorithm
●
Running over characters
●
Counting each transition from blanc to non-blanc characters
– Input
String text = "Twinkle twinkle little star";
NMCGJ
2020-2021
Strings and Arrays
●
Java program: string manipulation
– Counting the number of words in a string
int i = 0, wordCount = 0;
}
NMCGJ
2020-2021
Strings and Arrays
●
Java program: string manipulation
– Counting the number of words in a string
T
int i = 0, wordCount = 0;
O
i++;
i++;
T C
while (text.charAt(i) != ' ') // skip non-blanc chars
} NO
wordCount++;
NMCGJ
2020-2021
Strings and Arrays
●
Java program: string manipulation
– Counting the number of words in a string
int i = 0, wordCount = 0;
●
Characters and strings
– Formatting output (since Java SE5)
●
System.out.printf(), System.out.format(), String.format( )
●
Characters and strings
– Format specifiers
%<flags><width><.precision><conversion>
optional
●
conversion
Code Meaning Code Meaning
d integer (decimal) c character (Unicode)
x integer (hexadecimal) s string
f floating point (decimal) b boolean
e floating point (scientific) % literal “%”
NMCGJ
2020-2021
Strings and Arrays
●
Characters and strings
– Format specifiers
%<flags><width><.precision><conversion>
optional
●
width: minimum size of a field (padding with space chars)
●
flags: controls alignment
– default: right alignment
– using ‘−’: left alignment
●
Precision: meaning depends on type
– strings: maximum number of characters
NMCGJ
– floating points: number of decimal places after comma (default = 6) 2020-2021
Strings and Arrays
●
Java program: string formatting
– Task
●
Printing a shopping receipt to the console
– Output
NMCGJ
2020-2021
Strings and Arrays
●
Java program: string formatting
– Printing a shopping receipt to the console
●
Java program: string formatting
– Printing a shopping receipt to the console
●
Java program: string formatting
– Printing a shopping receipt to the console
●
Java program: string formatting
– Printing a shopping receipt to the console
●
Arrays
– An array is a sequence of elements of same type (primitives / objects)
– Declaration
– Number of elements
<integer> = <array name> . length ;
NMCGJ
2020-2021
Strings and Arrays
●
Arrays
– Initialization
●
Arrays
– Initialization
●
Default value for primitives: zero
●
Default value for objects: empty object
NMCGJ
2020-2021
Strings and Arrays
●
Arrays
– Repetition statement: foreach loop (since Java SE5)
●
Java program: arrays
– Task
●
Searching maximum element in an unsorted array
– Algorithm
●
Running over elements
●
Comparing current element > maximum value so far
– Input
int[] list = {24, 14, 10, 30, 45, 2,
1, 23, 5, 34, 23, 8};
NMCGJ
2020-2021
Strings and Arrays
●
Java program: arrays
– Searching maximum element in an unsorted array
●
Java program: arrays
– Searching maximum element in an unsorted array (bis)
NMCGJ
2020-2021
Strings and Arrays
●
Two-dimensional arrays
– A two-dimensional array is an array of arrays
●
Arrays are objects
●
Objects can be elements in an array
– Creating a matrix
<type>[][] <matrix name> = new <type>[<integer>][<integer>];
NMCGJ
2020-2021
Strings and Arrays
●
Two-dimensional arrays price
– Example
price
– Number of rows and columns
price[1]
int r = price.length; = 3 price[1][0]
int c = price[0].length; = 2
NMCGJ
2020-2021
Strings and Arrays
●
Two-dimensional arrays
– A two-dimensional array is not always a matrix!
price
int[][] price = new int[3][];
price[0] = new int[3];
price[1] = new int[2];
price[2] = new int[1];
– Multi-dimensional arrays
●
straightforward extension
NMCGJ
2020-2021
Strings and Arrays
●
Two-dimensional arrays
– Example: printing a given matrix to the console
NMCGJ
2020-2021
Strings and Arrays
●
Two-dimensional arrays
– Example: printing a given matrix to the console (bis)
System.out.println("matrix = ");
for (int[] row : matrix) {
System.out.print(" [");
for (int entry : row)
System.out.printf("[%d]", entry);
System.out.println("]");
}
NMCGJ
2020-2021
Strings and Arrays
●
Container classes
– Arrays
●
Most efficient way to hold a group of objects (compiler supported)
●
Primitives or objects of same type
●
Fixed size
– Container classes
●
Part of the library java.util
●
Sophisticated ways to hold your objects
– Exploiting characteristics of your group (List, Set, Map, ...)
●
Objects of same type (based on the concept of generics)
●
Automatically resizing
NMCGJ
2020-2021
Strings and Arrays
●
Container classes
– Overview of basic interfaces
●
Collection: a sequence of elements according to some rules
– List: holds the elements in the way they were inserted
– Set: cannot have duplicate elements
– Queue: holds the elements in the order of a queuing discipline
●
Map: a group of key-value object pairs
– Look up an object using another object (principle of dictionary)
●
These interfaces allow you to hold your objects
without caring about the exact implementation
NMCGJ
2020-2021
Strings and Arrays
●
Container classes
– Overview of basic interfaces
●
Collection: a sequence of elements according to some rules
– List: holds the elements in the way they were inserted
– Set: cannot have duplicate elements
– Queue: holds the elements in the order of a queuing discipline
●
Map: a group of key-value object pairs
– Look up an object using another object (principle of dictionary)
●
These interfaces allow you to hold your objects
without caring about the exact implementation
NMCGJ
2020-2021
Strings and Arrays
●
Container classes
– Implementations of the interface List
●
ArrayList: extends the classical array generics: specify type of
– Fast random access to elements objects to be held in List
– Expensive insertion / deletion of elements in the middle between angle brackets
●
LinkedList (since Java SE5)
– Optimal sequential access, but slow random access
– Inexpensive insertion / deletion of elements in the middle
– Syntax of List creation
List<Apple> apples = new ArrayList<Apple>();
List<Orange> oranges = new LinkedList<Orange>();
NMCGJ
2020-2021
Strings and Arrays
●
Container classes
– Some useful List methods
●
Container classes
– Example: iterating over a List (using for loop)
class Apple {}
get() is fast for ArrayList
class Gala extends Apple {}
class Fuji extends Apple {} but slow for LinkedList
NMCGJ
2020-2021
Strings and Arrays
●
Container classes
– The interface Iterator is designed for
●
moving efficiently through a sequence (Collection), and
●
selecting each object in the sequence without knowing its structure
– Implementation depends on the specific container
– Some useful Iterator methods
●
Container classes
– Example: iterating over a List (using an Iterator)
NMCGJ
2020-2021
Strings and Arrays
●
Container classes: overview
Collection Map
PriorityQueue LinkedHashSet
NMCGJ
2020-2021