Array - Lecture notes 1-3 PDF

Title Array - Lecture notes 1-3
Author Gigi Marasigan
Course BS Information Technology
Institution AMA Computer University
Pages 13
File Size 855 KB
File Type PDF
Total Downloads 89
Total Views 176

Summary

ARRAY...


Description

Computer Programming 2 Array

1

Java Array This module includes topics on: 1. Array 2. Multidimensional array

Java Array In this module we are going to discuss the concept of an array and how to use it in a Java program. At the end of the lesson, the student should be able to:     

Discuss what is Java array. Declare and create an array. Use array in Loops Declare and create a multidimensional array. Create a program that implements Arrays.

Introduction to Array In the previous lesson, we have been working with variables that a store single value at a time. Variables can only hold one value, for example, in an integer variable you can assign single number and String variable can be assigned one long string of text. An array can hold multiple same type value at a time, just like a list of items. An array is a collection of variables of the same type. It has fixed number of values and its length is defined upon creation, therefore after creating an array its length is fixed. For example, an array of int is a collection of variable of type int and each variable in the collection has indexed and can hold different int value. Below is the illustration of a Java array:

The figure above is an array of 8 elements. Each item is called an element and an element has an index number and it used to access the value of an element. An array always starts with index 0 and the maximum index number is n-1 (where n is the length of an array), that is why the maximum index of the array of 8 elements is 7. For example, to get the value of the 3rd element (38), you can access it at index 2. Course Module

Declaring Array Declaring an array is similar to variable declaration. To declare an array, you need to specify the type, followed by a pair of square brackets and, and then followed by array name or identifier, for example, int[] intArray; or you can put the pair of square brackets after the identifier, int intArray[]; where type is the data type of the elements; the pair of square brackets are special symbols that indicates that this variable is an array. An array name can be anything you want, provided that it follows the rules of valid identifier. The size of the array was not specified because the declaration does not actually create an array, it only tells the compiler that this variable will hold an array of the specified type. The following are the examples of array declaration in other types: byte[] byteArray; short[] shortArray; long[] longArray; float[] floatArray; double[] doubleArray; boolean[] booleanArray; char[] charArray; String[] anArrayOfStrings; After declaring the array, we can now create it and specify its length. One way to create an array is with the new keyword. Always remember that the size of an array is not allowed to be changed once you have created the array. For example, //array declaration int[] intArray; //array creation intArray = new int[10] that can be also written as, int[] intArray = new int[10];

Computer Programming 2 Array

3

Below is the representation of the declared array above:

The example above shows how to declare and create an array. Based from the example, the array has a name of intArray and it can hold 10 elements with the int data type. Now that we have created an array we can now assign values to it. To assign values to the array, we need to access each element and use the assignment operator. To access the element, we need to specify the array name followed by opening bracket, then the index of the element and then closing bracket. For example, //declaration and creation of an array int[] intArray = new int[10]; intArray[0] = 10;

//assign 10 to the element index 0

intArray[1] = 8;

//assign 8 to the element index 1

intArray[2] = 38;

//assign 38 to the element index 2

intArray[3] = 80;

//assign 80 to the element index 3

intArray[4] = 3;

//assign 3 to the element index 4

intArray[5] = 13;

//assign 13 to the element index 5

intArray[6] = 10;

//assign 10 to the element index 6

intArray[7] = 21;

//assign 21 to the element index 7

intArray[8] = 5;

//assign 5 to the element index 8

intArray[9] = 3;

//assign 3 to the element index 9

There is another way to declare, create and assign values to the array at once: int[] intArray = { 10, 8, 38, 80, 3, 13, 10, 21, 5, 3 }; Course Module

Below is the representation of the examples above,

Each element has now an assigned value, therefore we can now use the value by accessing the element through its index. For example, System.out.println("First Element at index 0: " + intArray[0]); System.out.println("Fifth Element at index 4: " + intArray[4]); System.out.println("Tenth Element at index 2: " + intArray[9]); Let us now create a program that implements Java array:

Below is the output of the sample program above:

Computer Programming 2 Array

5

Here’s another example of Java program that shows another way of assigning values to an array:

Below is the output of the sample program:

Array Length Length is a property of array objects that you can use to get the size of the array. Below is the example of getting the length of an array: int[] intArray = new int[100]; int arrayLength = intArray.length; From the example above, we have created an array with the size of 100. We also declared variable arrayLength that was assigned with intArray.length this means that the variable arrayLength will contain the value 100.

Course Module

Array and Loop Loops are useful in an array, for example, we have to assign a series of numbers in an array or we need to traverse all the elements of an array. Here is an example of assigning and iterating an array with a for loop: int[] intArray = new String[5]; for(int i=0; i < intArray.length; i++) { intArray[i] = i+1; } for(int i=0; i < intArray.length; i++) { System.out.print (intArray[i]); } From the example above, the first statement creates an array of int with the length of 5. Initially, each element has no value, the first loop statements assigns value to each element of intArray. In the loop statement, the variable i is initialized to 0 and runs up until the length of the array minus 1. In this case, variable i will have the values 0 through 9, and for each iteration variable i has different value. The statement intArray[i] = i + 1;, this assigns values to each element in the array. Instead of using number between the square brackets of the array, we specified the variable i. This increases by 1 for each iteration of the loop, therefor every time the variable i increases the position or index of the array element that we access also increases. Now, for every iteration in the loop, each element will have the value of i+1. So, the value of each element in the array is just the incremented loop value. The intArray has now the values of 1 to 10. The second loop statement will also traverse the intArray and display all the value of each element. When we execute the example code above it will display 1 2 3 4 5 6 7 8 9 10. Let us create a program that traverse an array using for loop:

Computer Programming 2 Array

Below is the output of the sample program above:

Course Module

7

Let’s have another example of array, but this time we are going to use array of String.

Below is the output of the sample code above:

Computer Programming 2 Array

9

The program displays the values of the array (line 13 – 15) and prompts the user to enter a name to search (line 18 – 20), if the name that the user has entered is present in the array (names) the program will display the index position of the matching element, and if the name is not present it will do nothing.

Multidimensional Array Multidimensional array is implemented as arrays of arrays. Multidimensional arrays are declared by appending the two or more bracket pairs after the array name or data type. Below is the example of two-dimensional array: int[][] int2DArray = new int[5][10]; This example creates a two-dimensional array of int elements. The array contains 5 elements in the first dimension, and 10 elements in the second dimension. The array of arrays has space for 5 int arrays, and each int array has space for 10 int elements. We can access the elements in a multidimensional array with one index per dimension (bracket pair). The example above is a two-dimensional array therefore we have to use two indices to access each element. Here is an example on how we assign values in a multidimensional array: //creates two-dimensional array int[][] int2DArray = new int[2][3]; //assign the value of 1 to an element at index 0 and 0 int2DArray[0][0] = 1; Course Module

int2DArray[0][1] = 2; int2DArray[0][2] = 3; int2DArray[1][0] = 4; int2DArray[1][1] = 5; int2DArray[1][2] = 6; Another way to assign the above values in a multidimensional array is: int[][] int2DArray = {{1,2,3},{4,5,6}}; Here’s an example of Multidimensional Array in Java program:

Below is the out of the sample program above:

Computer Programming 2 Array

11

Traversing Multidimensional Array using Loop To iterate a multidimensional array, we need to iterate each dimension of the array separately, therefore we need to use nested loop to traverse the multidimensional array. Below is the example of a java program that traverses a multidimensional array:

Course Module

Below is the out of the sample program above:

The first nested loop is used to display the array of int while the other one is used to display the array of String. For every iteration of the outer loop is a complete loop of the inner loop, in other words, for every iteration of outer loop the statement System.out.println("int2DArray[" + i + "][" + j + "]: " + int2DArray[i][j]); is executed 3 times. On the first outer loop iteration the value of i is 0 and the value of j is from 0 to 2, that is why we will have an output of: int2DArray[0][0]: 1 int2DArray[0][1]: 2 int2DArray[0][2]: 3 On the second outer loop iteration the value of i will become 1 and the value of j is also 0 to 2, and we will have an output of : int2DArray[1][0]: 4 int2DArray[1][1]: 5 int2DArray[1][2]: 6 Now, on the last outer loop iteration the value of i will become 2 and the value of j is again 0 to 2, and we will have an output of : int2DArray[2][0]: 7 int2DArray[2][1]: 8 int2DArray[2][2]: 9

Computer Programming 2 Array

The same process will be applied on the other nested loop and we can get this output: string2DArray[0][0]: a string2DArray[0][1]: b string2DArray[0][2]: c string2DArray[1][0]: d string2DArray[1][1]: e string2DArray[1][2]: f

Course Module

13...


Similar Free PDFs