0% found this document useful (0 votes)
8 views

W7-Presentation-Java Array

The document provides an introduction to Java arrays, explaining their fixed size, declaration, creation, and how to access and assign values to elements. It also covers multidimensional arrays and their declaration, as well as methods for traversing arrays using loops. Examples are provided throughout to illustrate the concepts discussed.

Uploaded by

igcasan.jc07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

W7-Presentation-Java Array

The document provides an introduction to Java arrays, explaining their fixed size, declaration, creation, and how to access and assign values to elements. It also covers multidimensional arrays and their declaration, as well as methods for traversing arrays using loops. Examples are provided throughout to illustrate the concepts discussed.

Uploaded by

igcasan.jc07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Java Array

Introduction to Array
- 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.

- it can hold multiple same type value at a time, just like a list
of items.
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:
Declaring Array
- 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.
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];


Below is the representation of the declared array above:
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 };

Below is the representation of the examples above,


Below is the output of the sample program above:

Below is the output of the sample program above:


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.

int[] intArray = new int[100];


int arrayLength = intArray.length;
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.

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]);
}
Let us create a program that traverse an array using for loop:

Below is the output of the sample program above:


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:
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];

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;

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:


Traversing Multidimensional Array using
Loop
Below is the example of a java program that traverses a multidimensional
array:
Below is the out of the sample program above:
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

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

You might also like