This document discusses arrays in Java. It defines an array as a way to store multiple values in a single variable instead of separate variables. It explains how to declare and initialize arrays in Java, access array elements, change element values, determine the length of an array, loop through an array, and create multidimensional arrays. The document also discusses passing arrays to methods and using a foreach loop to traverse an array.
3. 3
Array
Arrays are used to store multiple values in a
single variable, instead of declaring separate
variables for each value.
4. Java Array
4
In Java, to declare an array, define the variable type with square brackets:
String[] cars;
To insert values to it, we can use an array literal - place the values in a
comma-separated list, inside curly braces:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
5. Access the Elements of an Array
You access an array element by referring to the index number.
This statement accesses the value of the first element in cars:
5
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
6. Change an Array Element
6
To change the value of a specific element, refer to the index number:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
7. 7
Array Length
To find out how many elements an array has, use the length property:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4
8. Loop Through an Array
8
You can loop through the array elements with the for loop, and use the
length property to specify how many times the loop should run.
The following example outputs all elements in the cars array:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
// Outputs Volvo, BMW, Ford, Mazda
9. Multidimensional Arrays
A multidimensional array is an array
containing one or more arrays.
To create a two-dimensional array, add each
array within its own set of curly braces:
9
10. Access the Element of an Array
10
To access the elements of the myNumbers array, specify two indexes:
one for the array, and one for the element inside that array.
This example accesses the third element (2) in the second array (1) of
myNumbers:
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
11. 11
Loop Through an Array
public class MyClass {
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}
}
}
13. Create an Array
13
You can create an array by using the new operator with the following
syntax:
arrayRefVar = new dataType[arraySize];
The above statement does two things:
• It creates an array using new dataType[arraySize].
• It assigns the reference of the newly created array to the variable
arrayRefVar.
dataType arrayRefVar[];
15. The Foreach Loops
15
JDK 1.5 introduced a new for loop known as foreach loop or enhanced
for loop, which enables you to traverse the complete array sequentially
without using an index variable.
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
16. 16
Example
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList) {
System.out.println(element);
}
}
}
17. Passing Arrays to Methods
17
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
//printArray(new int[]{3, 1, 2, 6, 4, 2});
Just as you can pass primitive type values to methods, you can also pass
arrays to methods. For example, the following method displays the
elements in an int array:
18. Thank You, Next …
Relation between Classes
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2019-2020
Andi Nurkholis, S.Kom., M.Kom.
March 19, 2020