03 - Strings and Arrays
03 - Strings and Arrays
Hsiu-Chin Lin
do {
▪ Is there a do-while in Java?
statement(s)
▪ Yes, it looks like other language
} while (expression);
double d = 3.0 ;
▪ How do we initialize float?
float f = 3.0f ;
TYPE CASTING
TYPECASTING
int x = 3; // x is an integer
double y = 4.56; // y is a double
int n = (int) y; // convert y to integer
double m = (double) x; // convert x to double
int x = 3; // x is an integer
double m = (double) x; // convert x to double
double n = x; // it's ok to skip (double)
double 64
float 32
long 64
wider narrower
int 32
Here, wider usually
(but not always)
char 16
means more bytes. short 16
byte 8
Widening and narrowing change the bit representation (Details in ECSE 222 and
324)
NOTE: char is "special"... see the following slides.
EXAMPLES
int i = 3;
double d = 4.2;
c = (char) x; // narrowing
short y = 12;
String s = "Review";
String s = "" + 4;
➢ To convert from a String to an int, use:
int x = Integer.parseInt("54");
String s = "5";
int y = Integer.parseInt(s);
type[] variable_name;
type[] variable_name;
NOTE: The declaration
creates a variable, but
▪ Examples: we still need to
String[] days; create the actual
array object itself!
int[] grades;
double[] heights;
String[] args;
ARRAYS –EXAMPLE
Here is a list of the days of the week:
Saturday
Sunday
ARRAYS –INITIALIZATION METHOD 2
▪ Step 1: use the new operator to create an array of a certain size.
Examples:
0x00FF null
String[] days = new String[7];
days null
null
String[] days;
days = new String[7]; null
null
int numOfDays = 7; null
String[] days; null
Examples
days[1] = "Tuesday"; 0x00FF Monday
days[0] = "Monday";
days Tuesday
days[6] = "Sunday";
days[5] = "Saturday"; Wednesday
days[2] = "Wednesday"; Thursday
days[3] = "Thursday";
Friday
days[4] = "Friday";
Saturday
Sunday
DEFAULT VALUES
▪ Any integer expression can be used for the size of an array as long as the
value is nonnegative. If negative: NegativeArraySizeException.
ACCESSING ELEMENTS
▪ Elements in an array can be access using the name of the array variable
and the index of the element inside square brackets.
▪ days[0] = "Sunday";
The value "Sunday" is assigned to days at index 0
▪ String lastDay = days[6];
The value in days at index 6 is assigned to the variable lastDay
▪ days[0] = days[6];
The value in days at index 6 is assigned to days at index 0.
▪ If we use an index that is negative or grater than the size of the array
minus 1, the result is an ArrayIndexOutOfBoundsException.
ARRAYS AND LOOPS Note the
difference!
double sum = 0;
for (int i = 0; i < grades.length; i++) {
sum += grades[i];
}
double avg = sum/grades.length;
COMMON MISTAKES
System.out.println(a[0]);
▪ Output: 0
COMMON MISTAKES
▪ ArrayIndexOutOfBounds exception
COMMON MISTAKES
int size = -3 % 5;
String[] petNames = new String[size];
▪ NegativeArraySizeException exception
COMMON MISTAKES
0x00FF 1 0x0EFE 1
x 2 y 2
3 3
int[] x = {1,2,3};
int[] y = {1,2,3};
System.out.println(x.equals(y));
false
HOW TO CHECK EQUALITY?
int[] x = {1,2,3};
int[] y = {1,2,3}; true
System.out.println(java.util.Arrays.equals(x,y));
▪ Java.util.Arrays
▪ Arrays is a class that contains methods for manipulating arrays.
▪ Arrays is part of the java.util package.
▪ https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
THE IMPORT STATEMENT
▪ The import statement for the Arrays class:
import java.util.Arrays;
▪ All import statements are at the beginning of the file, before the class
definition.
THE IMPORT STATEMENT
import java.util.Arrays ;
▪ Arrays.toString(x)
It returns the content of an array as a String.
▪ Arrays.sort(x)
It sorts the input array in increasing order.
MULTIDIMENSIONAL
ARRAYS
ARRAYS
0 0 1
numbers 1 the second an array of length 3
1 2
(with values 1, 2, 3)
2 2 3
1 0
0
numbers 1 0 0
2 1 0
▪ This creates an array of length 3.
▪ Each element is an integer array of length 2. 0 0
0 0 1
numbers 1 1 2
2 2 3
int[] second = {1,2,3};
int[] third = {1, 2} ; 0 1
numbers[1] = second; 1 2
numbers[2] = third ;
MUTABLE V.S. IMMUTABLE
0 1
1 2
num 0 2 3
1
0 0
2
1 0
0 5
2D ARRAYS
0 1
1 2
num 0 2 3
1
0 -6
2
Note: 1 0
• We changed an element of num[1]. num[1]
is an array and arrays are mutable → the
reference stored in num[1] did not change! 0 5
NOTE ON EQUALITY
▪ If you want to compare the elements of the inner arrays you should use
Arrays.deepEquals(array1, array2)
MULTIDIMENSIONAL ARRAYS
▪ You can create arrays with as many dimensions as you like.