Object-Oriented Programming Module 5
Object-Oriented Programming Module 5
WEEK 6 - 7
6.1 Arrays
In this chapter we'll learn how to deal with Java arrays. Using examples, we'll learn how to declare,
initialize, and access array elements.
An array is a set of related data types. It is a container which holds one type of data (values). You can
create an array, for example, that can hold 100 values of type int.
In Java, arrays are a fundamental construct allowing you to easily store and access a large number of
values.
dataType = Primitive data types, such as int, char, double, bit, etc., or Java objects
arrayName = it is an identifier
Fair question anyway! We will assign the array to memory. The memory determines the number of
elements the array can contain.
In this case the array size is 10. That means it can keep up to 10 elements (10 values for double types).
The size of an array is also called the array length.
Note: This can not be changed in the program once the length of the array is defined.
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
In Java, we are able to declare and assign array memory in a single sentence. For instance,
int[] age = new int[5];
Here, we have an array of length 5. We can see in the image that each element is composed of a
number (array index). The indices on the array always start with 0.
Now, we are able to use the index number to access array elements. For example, we can use age[0] to
access the first element in the sequence, and access the second element using age[1], and so on.
Note: If the length of an array is n, arrayName[0] will be the first element in the array and arrayName[n-1]
will be the last component.
If we have not stored any value in an array, the array will store a certain default value (0 for type int
and false for type boolean) by itself. For instance,
class ArrayExample {
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
System.out.println(age[0]);
System.out.println(age[1]);
System.out.println(age[2]);
System.out.println(age[3]);
System.out.println(age[4]);
Output:
In the example above, we generated an array called era. We did not assign any values to the array,
however. Therefore the default values are printed to the screen when we reach the individual elements of the
series.
Here, the elements of the collection are accessed individually. There is a simpler way of using a loop to
access array elements (generally for-loop). For instance,
class ArrayExample {
System.out.println(age[i]);
Output:
This statement generates an array called age and initializes it in the curly brackets with the value given.
The array length is determined by the number of values within the curly braces, separated by commas.
In our case, the age limit is 5.
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
// create an array
Output:
Element at index 0: 12
Element at index 1: 4
Element at index 2: 5
Element at index 3: 2
Element at index 4: 5
age[2] = 14;
age[0] = 34;
Output:
Element at index 0: 34
Element at index 1: 0
Element at index 2: 14
Element at index 3: 0
Element at index 4: 0
int sum = 0;
Double average;
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
sum += number;
Output:
Sum = 36
Average = 3.6
In the above example, we have created an array of named numbers. We have used the for...each loop to
access each element of the array.
\We calculate the sum of every element within the loop. Remark the line,
Here we use the array's longitude attribute to measure the array 's height.
As you can see, the int value is transformed into a double. In Java this is called category casting.
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
We generated a multi-dimensional array called a here. It is a 2-dimensional array, which can hold up to
12 elements,
Note, Java uses zero-based indexing, that is, Java array indexing begins with 0 rather than 1.
Let's take one more example of Multidimensional Collection. We are constructing a 3-dimensional
sequence this time. For example,
String[][][] data = new String[3][4][2];
Here, data is a 3d array that can hold a maximum of 24 (3*4*2) elements of type String.
{1, 2, 3},
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
{4, 5, 6, 9},
{7},
};
As we can see, every element in the multidimensional array is itself an array. And also, unlike C / C++,
every row in Java's multidimensional array can be of different lengths.
class MultidimensionalArray {
public static void main(String[] args) {
// create a 2d array
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
Output:
Length of row 1: 3
Length of row 2: 4
Length of row 3: 1
In the above example, a multidimensional array named “a” is being generated. Since any part of a
multidimensional array is also an array (a[0], arrays are also a[1] and a[2]).
We use the longitudinal attribute here to measure the length of each row.
class MultidimensionalArray {
int[][] a = {
{7},
};
System.out.println(a[i][j]);
}
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
Output:
-2
-4
-5
We can also use the for ... each loop to access multidimensional array elements. For instance,
class MultidimensionalArray {
// create a 2d array
int[][] a = {
{7},
};
System.out.println(data);
}
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
Output:
-2
-4
-5
We have generated a 2d array called an in the above example. We then used to access each part of the
array for loop and for ... each loop.
// test is a 3d array
int[][][] test = {
{2, 3, 4}
},
{1},
{2, 3}
}
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
};
A 3d array is basically a collection of 2d arrays. The length of a 3d array's rows can also differ just as in a 2d array.
// create a 3d array
int[][][] test = {
{2, 3, 4}
},
{1},
{2, 3}
};
System.out.println(item);
}
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
Output:
-2
-4
-5
System.out.println(intArray);
// [I@7150bd4d
System.out.println(intArrayString);
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
// [1, 2, 3, 4, 5]
System.out.println(arrayList);
// [a, b, c, d, e]
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true
int[] intArray2 = { 6, 7, 8, 9, 10 };
System.out.println(j);
// a, b, c
arrayList.toArray(stringArr);
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
System.out.println(s);
System.out.println(set);
//[d, e, b, c, a]
9. Reverse an array
int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]
System.out.println(Arrays.toString(removed));
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
SELF-EVALUATION
1. Examine Java code below. What is the name of the string variable which is converted to a byte array?
a. getBytes c. loginName
b. Undefined d. loginBytes
2. What is the name of the method being used to display a readable version of the byte array coolArray in
this Java code?
a. getBytes c. toString
b. getString d. System.out.print
3. When converting a string to a byte array in Java, the values in each bucket of the array are .
a. EBSIDEC c. ASCII
4. An array in Java that would have rows and columns is called a _____ array?
a. Cubic c. Two-dimensional
b. Sequence d. Three-dimensional
a. String c. Double
b. Integer d. Character
a. 10 20 30 40 50
b. Compile Error
c. 10 20 30 40
a. 0 0 c. Compile Error
garbage value
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
a. 0 0 c. Compile Error
garbage value
a. Compile error
b.
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
c.
d.
PAMANTASAN NG LUNGSOD NG MUNTINLUPA
LEARNING COLLEGE OF INFORMATION TECHNOLOGY
MODULE
AND COMPUTER STUDIES
University Road, Poblacion, Muntinlupa City
LABORATORY EXERCISES
Machine Problem 1:
Write a Java program to remove a given array's duplicate elements, and return the new array
length.
Sample array: [20, 20, 30, 40, 50, 50, 50]
After removing the duplicate elements the program should return 4 as the new length of the
array.
Machine Problem 2:
Write a Java program to find smallest and second smallest elements of a given array.
Machine Problem 3:
Write a Java program to find the second largest element in an array
Machine Problem 4:
Write a Java program to find the second smallest element in an array
Machine Problem 5:
Write a Java program to find the number of even and odd integers in a given array of integers.