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