COS202 Lecture 3
COS202 Lecture 3
LECTURE 3
JAVA ARRAY
Introduction
Data Structure is a systematic way to organize data in order to use it efficiently. Data Structure is
about rendering data elements in terms of some relationship, for better organization and storage.
For example, we have some data which has player's name "Alexa" and age 26. Here "Alexa" is
of String data type and 26 is of integer data type.
Java provides a data structure, the array, which stores a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same type. Instead of declaring
individual variables, such as number0, number1, ..., and number99, you declare one array
variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables.
Declaring Array
To declare an array, define the variable type with square brackets and variable to reference the
array.
Syntax
dataType[] variableName; // preferred way.
or
dataType variableName []; // works but not preferred way.
Note: The style dataType[] variableName is preferred. The style dataType variableName []
comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.
Creating Array
You can create an array by using the new operator with the following syntax:
variableName = 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 variableName.
Declaring an array variable, creating an array, and assigning the reference of the array to the
variable can be combined in one statement, as shown below:
dataType[] variableName = new dataType[arraySize];
Example 3.1
1 public class Main {
2 public static void main(String[] args) {
3 String[] fruits = {“Apple”, “Banana”, “Cucumber”, “Lemon”, “Mango”};
4
5 System.out.println(fruits[0]);
6 }
7 }
To find out how many elements an array has, use the length property. Example 3.2 shows the
length keyword being used in the Java program and also, to change the value of a specific
element by referring to the index number.
Example 3.2
1 public class Main {
2 public static void main(String[] args) {
3 String[] fruits = {“Apple”, “Banana”, “Cucumber”, “Lemon”, “Mango”};
4
5 fruits[0] = “Avocado”;
6
7 System.out.println(fruits[0]);
8 System.out.println(fruits.length);
9 }
10 }
Processing Arrays
When processing array elements, we often use either for loop or for-each loop because all of the
elements in an array are of the same type and the size of the array is known.
To loop through the array elements with the for loop, and use the length property to specify how
many times the loop should run.
Example 3.3
1 public class Main {
2 public static void main(String[] args) {
3 String[] fruits = {“Apple”, “Banana”, “Cucumber”, “Lemon”, “Mango”};
4
5 for (int i = 0, i < fruits.length, i++) {
6 System.out.println(fruits[i]);
7 }
9 }
10 }
There is also a for-each loop, which is used exclusively to loop through elements in arrays.
Syntax
for (type variable : arrayname) {
…
}
Example 3.4
1 public class Main {
2 public static void main(String[] args) {
3 String[] fruits = {“Apple”, “Banana”, “Cucumber”, “Lemon”, “Mango”};
4
5 for (String i : fruits) {
6 System.out.println(fruits[i]);
7 }
9 }
10 }
Example 3.4 can be read like this: for each String element (called i - as in index) in fruits, print
out the value of i. If you compare the for loop and for-each loop, you will see that the for-each
method is easier to write, it does not require a counter (using the length property), and it is more
readable.
CLASSWORK
1) Create an array of 12 elements and display the 5th, 8th and last element of the array
respectively.
2) Create a multidimensional array of two containers: Print the 3rd element in the second
container.
ASSIGNMENT
1) Using an array, write a Java program that takes 5 integers entered at runtime and displays all
the integers.
2) Write a Java program using an array consisting of integer elements: display all the elements
in the array and also display the sum of elements either using the for loop or for-each loop.