Professional Documents
Culture Documents
Formation Java Oca: Saber Bhar
Formation Java Oca: Saber Bhar
Arrays
Objects in Java (but not in C/C++).
Arrays
Arrays
Example:
int array of length 5.
Declaration:
int[] array1;
Initialization:
array1 = new int[5];
Arrays
Length provided can be an int variable.
Example:
int x = 6;
array1 = new int[x];
Arrays
Two ways to declare:
Arrays
Declare and initialize with some values:
Property “length”.
Arrays
Example: Printing out all the values of an array
Arrays
Another way of traversing:
for-each loop:
Arrays
What happens here?
System.out.println(array1[array1.length]);
Arrays
ArrayIndexOutOfBoundsException thrown
printArray( array1 );
Pass-by-reference:
Used for objects (such as arrays). Changing in the
method changes the original!
Pass-by-value
public void changeValue(float y) {
y = 3.4;
}
float y = 5.4;
changeValue(y);
System.out.println(y);
Pass-by-reference
public void changeValue(float[] y) {
y[y.length - 1] = 3.4;
}
Pass-by-reference
Copying arrays
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
System.arrayCopy(…)
Copying arrays
Example:
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i',
'n', 'a', 't', 'e', 'd'};
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(copyTo);
Copying arrays
Output: caffein
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i',
'n', 'a', 't', 'e', 'd'};
Multi-dimensional Arrays
Looked at 1-dimensional (1-D) arrays so far.
Multi-dimensional arrays:
Array of arrays!
Multi-dimensional Arrays
Examples:
Storing all student info in a 2-D array
Matrices
Storing 3D data such as points/vertices (x, y, z)
Multi-dimensional Arrays
Declaration (2-D arrays):
elementType[][] variableName;
Example:
float[][] matrix;
Multi-dimensional Arrays
Initialization:
matrix = new float[2][3];
2 rows, 3 columns
int[][] differentSize =
{{1, 4}, {3}, {9,8,7}};
Multi-dimensional Arrays
Declare and initialize with some values:
Multi-dimensional Arrays
Each row can have a different length
Multi-dimensional Arrays
float[][] matrix = new float[2][];
Multi-dimensional Arrays
Traversal:
Multi-dimensional Arrays
Traversal – Enhanced Loop:
System.out.println(bugs.toString());
//[Ljava.lang.String;@160bc7c0
Java - Saber BHAR 32
33
ArrayIndexOutOfBoundsException
numbers[10] = 3;
numbers[numbers.length] = 5;
for (int i = 0; i <= numbers.length; i++)
numbers[i] = i + 5;
Sorting
You can pass almost any array to Arrays.sort().
import java.util.* // import whole package
import java.util.Arrays; // import just Arrays
int[] numbers = { 6, 9, 1 };
Arrays.sort(numbers);
for (int i = 0; i < numbers.length; i++)
System.out.print (numbers[i] +
Java - Saber BHAR " "); 36
37
Searching
Java also provides a convenient way to search—
but only if the array is already sorted.
Searching
3: int[] numbers = {2,4,6,8};
4: System.out.println(Arrays.binarySearch(numbers, 2)); // 0
5: System.out.println(Arrays.binarySearch(numbers, 4)); // 1
6: System.out.println(Arrays.binarySearch(numbers, 1)); // -1
7: System.out.println(Arrays.binarySearch(numbers, 3)); // -2
8: System.out.println(Arrays.binarySearch(numbers, 9)); // -5
String s = “Java”;
s = “Another String”;
s Java
Now:
s Java
Another String
String comparisons
Interned instances of the same string
Example:
String s1 = “Java”;
String s2 = new String(“Java”);
String s3 = “Java”;
String comparisons
In Memory:
s1
Java
s2
Java
String comparisons
In Memory:
s1
Java
s3
s2
Java
String comparisons
s1 == s2 is ? (different reference)
String comparisons
s1 == s2 is false (different reference)
String comparisons
s1 == s2 is false (different reference)
s1 == s3 is ? (same reference)
String comparisons
s1 == s2 is false (different reference)
String comparisons
Don’t use “==“ for comparing objects!
Compares references
String comparisons
Don’t use “==“ for comparing objects!
Compares references
string1.equals(string2) for content comparison
s1.equals(s2) is true
String comparisons
Don’t use “==“ for comparing objects!
Compares references
string1.equals(string2) for content comparison
s1.equals(s2) is true
indexOf() - looks at the characters in the string and finds the first
index that matches the desired value.
Input
public static void main(String[] args) {
...
}
Input
public static void main(String[] args) {
...
}
Input
public static void main(String[] args) {
...
}
Executing a program:
java programName arg1 arg2 arg3 ...
Varargs
Here are three examples with a main() method
String Concatenation
Note the + operator can be used in two ways within the same line
of code. 3 rules :
If both operands are numeric, + means numeric addition.
If either operand is a String, + means concatenation.
The expression is evaluated left to right.
System.out.println(1 + 2) //3
System.out.println(“a” + ”b”) //ab
System.out.println(“a” + ”b” + 3) //ab3
System.out.println(1 + 2 + ”c”) //3c
Remember what + = does. s + = "2" means the same thing as s = s +
“2".
Java - Saber BHAR 64
65
String s1 = “Java”;
String s2 = new String(“Java”);
Creating a StringBuilder
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder("animal");
StringBuilder sb3 = new StringBuilder(10);
Understanding Equality
StringBuilder one = new StringBuilder();
StringBuilder two = new StringBuilder();
StringBuilder three = one.append("a");
System.out.println(one == two); // false
System.out.println(one == three); // true
Understanding Equality
Remember to never use == to compare Strings.
equals to check the values inside the String rather
than the String itself.
Review Questions
Question
Question
Question