Welcome to the spoken tutorial on Introduction to Arrays.
In this tutorial, you will learn how to create arrays and access elements in arrays.
For this tutorial, we are using: Ubuntu 11.10 JDK 1.6 and Eclipse 3.7.0
For this tutorial, you should have knowledge of data types and for loop in Java.
If not, for relevant tutorials, please visit our website which is as shown.
Arrays are a collection of data.
For example, a list of marks, a list of names, a list of temperatures, a list of rainfall.
Each item has an index, based on its position.
The index of the first element is 0.
The index of the second element is 1 and so on.
Let us now see how to store this data.
So switch to Eclipse.
A class named ArraysDemo has already been created.
Within the main method, let us add the rainfall data.
So, inside main function, type:
int rainfall open and close square brackets equal to within curly brackets type 25, 31, 29, 13, 27, 35, 12 and finally the semicolon.
Note that the square braces after the variable name rainfall.
This declares rainfall as an array of integers.
The braces are used to specify the elements of the array.
Let us now access data.
So on the next line, type:
System dot out dot println rainfall in square brackets type 2.
We are printing the element with the index number 2.
In other words, the third element in the array i.e. 29.
Let us save and run the program.
As we can see, the output is the third element, i.e 29.
Now, let us type 0 in place of 2.
Save and run the program.
As we can see, the output is the first value i.e 25.
Now, let us modify the value of the first item.
So, type: rainfall[0] = 11;
Now, let us see its value. So save and run the program.
As we can see, the value has been changed to 11.
Now, what if we know only the size of the array and do not know the values.
Let us see how to create such array.
Remove everything in main function and type:
int squares[] = new int[10];
This statement creates an array of integers having 10 elements. The name of the array is squares.
Now, let us add some values to it.
So, type:
squares[0] = 1;
Next line, squares[1] = 4;
Next line, squares[2] = 9;
squares[3] = 16;
So, we have entered the squares of first four numbers.
Now what about the other elements of the array. Let us see what they contain.
So, we shall print the sixth value in the array.
Type: System S capital .out.println(squares[5]);
Save and run the program. We see that the value is zero.
This is because when we create an array of integers, all the values are initialized to 0.
Similarly, an array of floats will have all its values initialized to 0.0.
It would be a long process if we have to type each value into the array. Instead, let us use a for loop.
So, type: int n, x ; for(x = 4; x < 10; x = x + 1){ n = x + 1; squares[x] = n * n; }
So, we iterate over numbers from 4 to 9 and set the corresponding element in the array.
Now, let us see the output.
As we can see, we are printing the value of sixth element in array. So, Save and run.
We see the sixth element is now square of 6 which is 36.
In fact, now we can set all the values inside the for loop.
Remove the lines that set the values manually and change 4 to 0.
This way, all the elements from index 0 to 9 are set to the corresponding squares.
We shall now see the value of the third element.
So, change 5 to 2.
Save and run.
As we can see, the value of the third element has been set in the loop and it is 9.
This way, arrays can be created and used.
We have come to the end of this tutorial.
In this tutorial, we have learnt:
To declare and initialize an array.
And access an element in an array.
The assignment for this tutorial is:
Given an array of integers, find the sum of all the elements in the array.
To know more about the Spoken Tutorial project,
watch the video available at the following link.
It summarizes the project. If you do not have good bandwidth, you can download and watch it.
The Spoken Tutorial project team: Conducts workshops using spoken tutorials and gives certificates to those who pass an online test.
For more details, please write to contact AT spoken HYPHEN tutorial DOT org.
Spoken Tutorial project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
More information on this mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro.
This script has been contributed by TalentSprint. This is Prathamesh Salunke, signing off. Thanks for joining.