Difference between revisions of "Java/C2/Array-Operations/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 69: | Line 69: | ||
|- | |- | ||
| 01:13 | | 01:13 | ||
− | | Let us now '''import''' the '''class Arrays'''. | + | | Let us now '''import''' the '''class''' '''Arrays'''. |
|- | |- | ||
Line 97: | Line 97: | ||
|- | |- | ||
| 02:03 | | 02:03 | ||
− | |'''int marks''' open and close square brackets equal to within brackets '''2, 7, 5, 4, 8 ''' | + | |'''int marks''' open and close square brackets equal to within curly brackets '''2, 7, 5, 4, 8 ''' |
|- | |- | ||
Line 105: | Line 105: | ||
|- | |- | ||
| 02:28 | | 02:28 | ||
− | |So, type: '''String mStr''' equal to ''' Arrays''' dot | + | |So, type: '''String mStr''' equal to ''' Arrays''' dot ''toString'' parentheses inside parentheses will give the array name i.e. '''marks'''. |
|- | |- | ||
Line 117: | Line 117: | ||
|- | |- | ||
| 02:58 | | 02:58 | ||
− | |So, type '''System ''' dot '''out''' dot '''println''' inside parentheses type '''mStr''' | + | |So, type '''System ''' dot '''out''' dot '''println''' inside parentheses type '''mStr;''' |
|- | |- | ||
Line 133: | Line 133: | ||
|- | |- | ||
| 03:31 | | 03:31 | ||
− | | So, before the line '''Arrays''' dot '''toString''' type '''Arrays''' dot '''sort''' within | + | | So, before the line '''Arrays''' dot '''toString''' type: '''Arrays''' dot '''sort''' within parentheses the Array name i.e '''marks'''. |
|- | |- | ||
| 03:46 | | 03:46 | ||
− | |So, the '''sort''' '''method''' in the '''Arrays class''' | + | |So, the '''sort''' '''method''' in the '''Arrays class''' sorts the elements of the '''array''' passed to it. |
|- | |- | ||
| 03:53 | | 03:53 | ||
− | |Now we are sorting the | + | |Now we are sorting the elements of the array '''marks''' and then printing the '''string''' form of it. |
|- | |- | ||
Line 189: | Line 189: | ||
|- | |- | ||
| 05:32 | | 05:32 | ||
− | |The next method we are going to look at | + | |The next method we are going to look at is '''copyOf'''. |
|- | |- | ||
Line 200: | Line 200: | ||
|- | |- | ||
| 05:48 | | 05:48 | ||
− | | And type: '''int marksCopy [];''' | + | | And type: '''int marksCopy[];''' |
|- | |- | ||
| 05:59 | | 05:59 | ||
− | | Next line, type: '''marksCopy = | + | | Next line, type: '''marksCopy = Arrays.copyOf(marks, 5);''' |
|- | |- | ||
Line 216: | Line 216: | ||
|- | |- | ||
|06:39 | |06:39 | ||
− | | The second is the no.of elements to copy over here | + | | The second is the no.of elements to copy, over here we will copy 5. |
|- | |- | ||
| 06:47 | | 06:47 | ||
− | |Then, in '''arrays''' dot ''' | + | |Then, in '''arrays''' dot '''toString''', change '''marks''' to '''marksCopy'''. |
|- | |- | ||
Line 259: | Line 259: | ||
|- | |- | ||
| 07:48 | | 07:48 | ||
− | |As we can see, the extra elements have been set to the default value | + | |As we can see, the extra elements have been set to the default value which is 0. |
|- | |- | ||
Line 299: | Line 299: | ||
|- | |- | ||
| 09:07 | | 09:07 | ||
− | |(0, 4) implies from index 0 to | + | |(0, 4) implies from index 0 to 3. |
|- | |- | ||
Line 315: | Line 315: | ||
|- | |- | ||
| 09:31 | | 09:31 | ||
− | |In this tutorial we have learnt: | + | |In this tutorial, we have learnt: |
|- | |- | ||
Line 323: | Line 323: | ||
|- | |- | ||
| 09:36 | | 09:36 | ||
− | |* Perform array operations like ''' | + | |* Perform array operations like '''toStrings''', '''sort''', '''copy''', '''fill'''. |
|- | |- | ||
Line 331: | Line 331: | ||
|- | |- | ||
| 09:46 | | 09:46 | ||
− | | Read about the Arrays. equals method and find out what it does. | + | | Read about the '''Arrays.equals''' method and find out what it does. |
|- | |- | ||
Line 353: | Line 353: | ||
|- | |- | ||
| 10:10 | | 10:10 | ||
− | | Conducts workshops using Spoken Tutorials, gives certificates | + | | Conducts workshops using Spoken Tutorials, gives certificates to those who pass an online test. |
|- | |- | ||
| 10:16 | | 10:16 |
Revision as of 10:26, 7 April 2015
Time | Narration |
00:02 | Welcome to the spoken tutorial on Array Operations in java. |
00:07 | In this tutorial, you will learn how to: |
00:09 | * import the class Arrays and |
00:12 | * perform basic operations on arrays. |
00:15 | For this tutorial, we are using
Ubuntu 11.10, JDK 1.6 and Eclipse 3.7.0 |
00:25 | For this tutorial, you should have knowledge of arrays in Java. |
00:30 | If not, for relevant tutorials, please visit our website which is as shown. [1] |
00:35 | The methods for array operations are available in a class called Arrays. |
00:40 | To access them, we need to import that class. |
00:43 | It is done by the statement import java.util.Arrays semicolon. |
00:50 | We can access a method from the class. |
00:52 | We do it by adding a 'dot' and the method name. |
00:56 | So, Arrays dot toString means toString method from the Arrays class. |
01:05 | Now switch to eclipse. |
01:08 | We have already created a class ArraysDemo. |
01:13 | Let us now import the class Arrays. |
01:16 | The import statement is written before the class definition. |
01:22 | So, before public class, type: |
01:26 | import java.util.Arrays semicolon |
01:46 | This statement says that java contains a package called util which contains the class Arrays and it has to be imported. |
01:59 | Now, let us add an array. |
02:01 | Inside the main function, type: |
02:03 | int marks open and close square brackets equal to within curly brackets 2, 7, 5, 4, 8 |
02:20 | Now we shall use a method available in the Arrays class to get a string representation of the array and print it. |
02:28 | So, type: String mStr equal to Arrays dot toString parentheses inside parentheses will give the array name i.e. marks. |
02:50 | Now, this toString method will give the string representation of the array. |
02:56 | We shall print the marks. |
02:58 | So, type System dot out dot println inside parentheses type mStr; |
03:12 | Now, let us look at the output. So save and run the program. |
03:18 | As we can see in the output, the toString method has given a string representation of the array. |
03:26 | Now, let us look at sorting the elements of the array. |
03:31 | So, before the line Arrays dot toString type: Arrays dot sort within parentheses the Array name i.e marks. |
03:46 | So, the sort method in the Arrays class sorts the elements of the array passed to it. |
03:53 | Now we are sorting the elements of the array marks and then printing the string form of it. |
04:04 | Let us look at the output. So save and run. |
04:11 | As we can see in the output, the sort method has sorted the array in the ascending order. |
04:19 | Note that the sort method has changed the array itself. |
04:22 | This type of sorting is called inplace sorting. |
04:26 | It means that the array which contains the elements is changed as a result of sorting. |
04:33 | The next method we are going to look at is fill. |
04:38 | The fill method takes two arguments. |
04:43 | Remove the sorting line and |
04:50 | type: Arrays dot fill within brackets the name of the array i.e marks; |
05:05 | This is our first argument and second is the value that should be filled in the array, we will give it 6 and semicolon. Save and Run. |
05:24 | As we can see, as the name goes, the fill method fills the array with the given argument i.e 6. |
05:32 | The next method we are going to look at is copyOf. |
05:37 | We are going to copy all the elements of the array marks into the array marksCopy. |
05:44 | So, remove 'arrays dot fill'. |
05:48 | And type: int marksCopy[]; |
05:59 | Next line, type: marksCopy = Arrays.copyOf(marks, 5); |
06:25 | This method takes two arguments. |
06:29 | The first argument is the name of the array from which you want to copy the elements.i.e marks. |
06:39 | The second is the no.of elements to copy, over here we will copy 5. |
06:47 | Then, in arrays dot toString, change marks to marksCopy. |
06:55 | Now save and run the program. |
07:01 | We see that the elements of the array marks have been copied to the array marksCopy. |
07:10 | Let us see what happens if we change the no.of elements to be copied. |
07:15 | Let's Change 5 to 3. |
07:19 | Save and Run. |
07:24 | As we can see, only the first three elements have been copied. |
07:31 | Let us see what happens if the no.of elements to be copied is greater than the total no.of elements in the array. |
07:39 | So, change 3 to 8. |
07:44 | Save and Run the program. |
07:48 | As we can see, the extra elements have been set to the default value which is 0. |
07:54 | Next, we'll see how to copy a range of values. |
07:58 | So, change copyOf to copyOfRange and 8 to 1, 4. |
08:15 | This method copies all the elements starting from the index 1 and stopping at index 3. |
08:27 | Save and Run. |
08:31 | As we can see, the elements from index 1 to 3 have been copied. |
08:39 | Note that we have given 1, 4 as our arguments. |
08:47 | But, even then the element at index 4 has not been copied. |
08:50 | Only the elements till index 3 have been copied. It stops one index before the given range. |
09:01 | So, this behavior ensures that continuity of ranges is maintained. |
09:07 | (0, 4) implies from index 0 to 3. |
09:12 | (4, 6) will imply index from 4 to 5. |
09:17 | So it behaves as if (0, 4) + (4, 6) = (0, 5). |
09:26 | This brings us to the end of this tutorial. |
09:31 | In this tutorial, we have learnt: |
09:33 | * How to import the class Arrays. |
09:36 | * Perform array operations like toStrings, sort, copy, fill. |
09:44 | For assignment: |
09:46 | Read about the Arrays.equals method and find out what it does. |
09:53 | To know more about the Spoken Tutorial project, |
09:55 | watch the video available at [2]. |
10:02 | It summarizes the Spoken Tutorial project. |
10:05 | If you do not have good bandwidth, you can download and watch it. |
10:09 | The Spoken Tutorial project team: |
10:10 | Conducts workshops using Spoken Tutorials, gives certificates to those who pass an online test. |
10:16 | For more details, please write to contact @ spoken HYPHEN tutorial DOT org. |
10:22 | 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. |
10:31 | More information on this mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro. |
10:39 | This tutorial has been contributed by TalentSprint. Thanks for joining. |
10:43 | This is Prathmesh Salunke, signing off. Jai Hind. |