Difference between revisions of "Java/C2/Introduction-to-Array/English"
Arya Ratish (Talk | contribs) |
|||
(One intermediate revision by one other user not shown) | |||
Line 59: | Line 59: | ||
− | For example, a list of marks, a list of names, a list of temperatures | + | For example, a list of marks, a list of names, a list of temperatures, a list of rainfall, |
Line 68: | Line 68: | ||
− | The second element has index 1 and so on. | + | The index of the second element has index 1 and so on. |
Line 97: | Line 97: | ||
'''int rainfall<nowiki>[] = {25, 31, 29, 13, 27, 35, 12};</nowiki>''' | '''int rainfall<nowiki>[] = {25, 31, 29, 13, 27, 35, 12};</nowiki>''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Inside main function, type | + | | style="border:1pt solid #000000;padding:0.097cm;"| So Inside main function, type |
− | '''int rainfall '''''open close | + | '''int rainfall '''''open close brackets equal to''''' '''''within curly brackets type''''' 25, 31, 29, 13, 27, 35, 12 ''''' and finally a semicolon.'' |
Line 111: | Line 111: | ||
The braces are used to specify the elements of the array. | The braces are used to specify the elements of the array. | ||
− | + | let us now access data. | |
− | + | ||
|- | |- | ||
Line 121: | Line 120: | ||
Highlight as you explain. | Highlight as you explain. | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Next line, type | + | | style="border:1pt solid #000000;padding:0.097cm;"| So Next line, type |
− | '''System '''''dot '''''out '''''dot'' '''println ''' | + | '''System '''''dot '''''out '''''dot'' '''println ''' ''' rainfall ''' in square brackets type 2 semicolon. |
− | We are printing the element with index 2. | + | We are printing the element with index number 2. |
− | In other words, the third element in the array. | + | In other words, the third element in the array i.e.29. |
− | Let us | + | Let us save run the program |
|- | |- | ||
Line 140: | Line 139: | ||
Highlight the ouput | Highlight the ouput | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| As we can see, the output is the third | + | | style="border:1pt solid #000000;padding:0.097cm;"| As we can see, the output is the third element,i.e '''29'''. |
− | Now let us | + | Now let us type 0 in place of 2 |
|- | |- | ||
Line 150: | Line 149: | ||
save and run. Point to output | save and run. Point to output | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Save and run the program |
+ | As we can see, the output is the first value i.e 25 | ||
− | |||
− | + | Now let us modify a valueof the first icon | |
− | Now let us modify a | + | |
|- | |- | ||
Line 168: | Line 166: | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Save |
− | + | ||
− | + | ||
Type '''rainfall<nowiki>[0] = 11;</nowiki>''' | Type '''rainfall<nowiki>[0] = 11;</nowiki>''' | ||
− | Now let us see its value. | + | Now let us see its value. So save and run the program |
Line 181: | Line 177: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| | | style="border:1pt solid #000000;padding:0.097cm;"| | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Now what if we | + | | style="border:1pt solid #000000;padding:0.097cm;"| 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. | Let us see how to create such array. | ||
Line 189: | Line 185: | ||
'''int squares<nowiki>[] = </nowiki>new int<nowiki>[10];</nowiki>''' | '''int squares<nowiki>[] = </nowiki>new int<nowiki>[10];</nowiki>''' | ||
This statement creates an array of integers having 10 elements. The name of the array is '''squares'''. | This statement creates an array of integers having 10 elements. The name of the array is '''squares'''. | ||
− | Now let us add some values | + | Now let us add some values to it |
− | + | ||
|- | |- | ||
Line 202: | Line 197: | ||
'''squares<nowiki>[3] = 16;</nowiki>''' | '''squares<nowiki>[3] = 16;</nowiki>''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| '''Type''' | + | | style="border:1pt solid #000000;padding:0.097cm;"|So '''Type''' |
Line 214: | Line 209: | ||
− | 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 have entered the squares of first four numbers. Now what about the other elements of the array. Let us see what they contain. |
|- | |- | ||
Line 220: | Line 215: | ||
'''System.out.println(squares<nowiki>[5]);</nowiki>''' | '''System.out.println(squares<nowiki>[5]);</nowiki>''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| We shall print the sixth value in the array. | + | | style="border:1pt solid #000000;padding:0.097cm;"|So We shall print the sixth value in the array. |
Line 230: | Line 225: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Save and run. Point to output | | style="border:1pt solid #000000;padding:0.097cm;"| Save and run. Point to output | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| 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. | + | | style="border:1pt solid #000000;padding:0.097cm;"| 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. |
Line 244: | Line 239: | ||
'''n = x + 1;''' | '''n = x + 1;''' | ||
− | '''squares<nowiki>[</nowiki>x] = n * n''' | + | '''squares<nowiki>[</nowiki>x] = n * n;''' |
'''}''' | '''}''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| '''Type''' | + | | style="border:1pt solid #000000;padding:0.097cm;"|So '''Type''' |
− | '''int n, x<nowiki>;</nowiki>''' | + | '''int n, x<nowiki>;</nowiki>''' |
'''for(x = 4; x<nowiki> < 10;</nowiki>x = x + 1){''' | '''for(x = 4; x<nowiki> < 10;</nowiki>x = x + 1){''' | ||
Line 256: | Line 251: | ||
'''n = x + 1;''' | '''n = x + 1;''' | ||
− | '''squares<nowiki>[</nowiki>x] = n * n''' | + | '''squares<nowiki>[</nowiki>x] = n * n;''' |
'''} ''' | '''} ''' | ||
Line 268: | Line 263: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Save and run. Point to output | | style="border:1pt solid #000000;padding:0.097cm;"| Save and run. Point to output | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| As we can see, the sixth element | + | | style="border:1pt solid #000000;padding:0.097cm;"| As we can see, we are printing the value of sixth element in array SoSave and run. We see the sixth element is now square of 6, which is 36. |
Line 291: | Line 286: | ||
− | This way all the elements from index 0 to | + | This way all the elements from index 0 to 9 are set to the corresponding squares. |
Line 297: | Line 292: | ||
− | change '''5 '''to '''2''' | + | So change '''5 '''to '''2''' |
Line 303: | Line 298: | ||
− | As we can see, the value of third element has been set in the loop and it is 9. | + | As we can see, the value of the third element has been set in the loop and it is 9. |
|- | |- | ||
Line 318: | Line 313: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| We have come to the end of this tutorial. | | style="border:1pt solid #000000;padding:0.097cm;"| We have come to the end of this tutorial. | ||
− | In this tutorial we have learnt | + | In this tutorial we have learnt |
− | + | TO declare and initialize the array , | |
+ | And access element in an array | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 8'''Assignment''' | | style="border:1pt solid #000000;padding:0.097cm;"| Slide 8'''Assignment''' |
Latest revision as of 10:26, 10 May 2013
Title of script: Introduction to Arrays
Author: TalentSprint
Keywords: arrays, java, video tutorial
Visual Cue | Description |
Slide 1
Welcome |
Welcome to the spoken tutorial on Introduction to Arrays. |
Slide 2
Learning Outcomes |
In this tutorial, you will learn how
|
Slide 3
Tools Used |
For this tutorial we are using
|
Slide 4
Prerequisites |
For this tutorial, you should have knowledge of data types and for loop in Java.
|
Slide 5 and 6
Arrays
|
Arrays are a collection of data.
|
Minimize Slides and open Eclipse
Eclipse should contain the following code public class ArraysDemo{ public static void main(String[] args){ } } |
A class named ArraysDemo has already been created.
|
Inside main function, type
int rainfall[] = {25, 31, 29, 13, 27, 35, 12}; |
So Inside main function, type
let us now access data. |
In the next line, type
System.out.println(rainfall[2]);
|
So Next line, type
|
Save and run.
|
As we can see, the output is the third element,i.e 29.
|
Change rainfall[2] to rainfall[0]
|
Save and run the program
As we can see, the output is the first value i.e 25
|
Before the Syste... line, type
rainfall[0] = 11;
|
Save
Type rainfall[0] = 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 | |
In the next line, type
squares[0] = 1; squares[1] = 4; squares[2] = 9; squares[3] = 16; |
So Type
squares[1] = 4; squares[2] = 9; squares[3] = 16;
|
In the next line, type
System.out.println(squares[5]); |
So We shall print the sixth value in the array.
|
Save and run. Point to output | 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.
|
After the squares[3] line, type
int n, x; for(x = 4; x < 10; x = x + 1){ n = x + 1; squares[x] = n * n; } |
So Type
for(x = 4; x < 10;x = x + 1){ n = x + 1; squares[x] = n * n; }
|
Save and run. Point to output | As we can see, we are printing the value of sixth element in array SoSave and run. We see the sixth element is now square of 6, which is 36.
|
Remove all the lines from
squares[0]... to squares[3]...
println(squares[2])
|
Remove the lines that set the values manually and change 4 to 0
|
This way, arrays can be created and used. | |
Minimize the Eclipse window and switch to slides.
Summary |
We have come to the end of this tutorial.
In this tutorial we have learnt TO declare and initialize the array , And access element in an array |
Slide 8Assignment | The assignment for this tutorial is,
|
Slide 9About the Spoken Tutorial Project
|
To know more about the Spoken Tutorial project, watch the video available at the following link. It summarises the project.If you do not have good bandwidth, you can download and watch it. |
Slide 10Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
Slide 11Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project and 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 |
Slide 12About the contributor
|
This script has been contributed by TalentSprint.
This is Prathamesh Salunke signing off. Thanks for joining.
|