Difference between revisions of "Java/C2/Array-Operations/English"
Pratham920 (Talk | contribs) |
|||
(5 intermediate revisions by one other user not shown) | |||
Line 51: | Line 51: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 5 | | style="border:1pt solid #000000;padding:0.097cm;"| Slide 5 | ||
− | ''' | + | '''import java.util.Arrays;''' |
| style="border:1pt solid #000000;padding:0.097cm;"| The methods for array operations are available in a '''class '''called '''Arrays'''. | | style="border:1pt solid #000000;padding:0.097cm;"| The methods for array operations are available in a '''class '''called '''Arrays'''. | ||
Line 58: | Line 58: | ||
− | It is done by the statement '''import java.util.Arrays''' | + | It is done by the statement '''import java.util.Arrays;''' |
Line 67: | Line 67: | ||
− | So '''Arrays '''''dot '''''toString''' means '''toString''' method from the | + | So '''Arrays '''''dot '''''toString''' means '''toString''' method from the '''Arrays''' class. |
|- | |- | ||
Line 84: | Line 84: | ||
'''}''' | '''}''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Switch to eclipse. | + | | style="border:1pt solid #000000;padding:0.097cm;"|Now Switch to eclipse. |
Line 96: | Line 96: | ||
'''import java.util.Arrays<nowiki>;</nowiki>''' | '''import java.util.Arrays<nowiki>;</nowiki>''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| The '''import''' statement is | + | | style="border:1pt solid #000000;padding:0.097cm;"| The '''import''' statement is written before the '''class '''definition. |
− | Before '''public class''', type | + | So Before '''public class''', type |
Line 117: | Line 117: | ||
− | '''int marks '''''open close square brackets''''' '''''equal to within brackets '''''2, 7, 5, 4, 8 ''' | + | '''int marks '''''open close square brackets''''' '''''equal to within brackets '''''2, 7, 5, 4, 8 ''''' |
|- | |- | ||
Line 129: | Line 129: | ||
− | + | SoType '''String mStr '''''equal to''''' Arrays '''''dot '''''toString ''''' Paranthesis inside Paranthesis ''''' will give the array name i.emarks '''''semicolon.'' | |
− | + | Now this '''toString''' method gives a string representation of the array. | |
− | + | ||
Line 138: | Line 137: | ||
− | So, type '''System '''''dot '''''out '''''dot '''''println '''' | + | So, type '''System '''''dot '''''out '''''dot '''''println ''''inside Paranthesis''''' mStr '''''semicolon.'' |
− | Now let us run | + | Now let us look at the output so save and run the program |
|- | |- | ||
| 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 '''toString''' method has given a string representation of the '''array.''' | + | | style="border:1pt solid #000000;padding:0.097cm;"| As we can see int the output, the '''toString''' method has given a string representation of the '''array.''' |
Line 163: | Line 162: | ||
− | Now we are sorting the array and then printing the string form of it. | + | Now we are sorting the element of the array '''marks''' and then printing the string form of it. |
− | Let us look at the output. | + | Let us look at the output.So save and run |
|- | |- | ||
| 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 | + | | style="border:1pt solid #000000;padding:0.097cm;"| As we can see in the output '''sort''' method has sorted to the array in the ascending order |
Line 190: | Line 189: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| The fill method takes two arguments. | | style="border:1pt solid #000000;padding:0.097cm;"| The fill method takes two arguments. | ||
− | + | ||
− | + | ||
Remove the sorting line and | Remove the sorting line and | ||
− | Type '''Arrays '''''dot '''''fill '''''within brackets''''' | + | Type '''Arrays '''''dot '''''fill '''''within brackets''''' the name of the arrays i.e marks;''' |
− | + | ||
− | + | ||
− | + | ||
+ | This is our first argument and second is the value that should be felt in the array we will give it 6 and semicolon. Save and Run | ||
+ | |||
|- | |- | ||
− | | 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, as the name goes, the fill method fills the array with the given argument. | + | | style="border:1pt solid #000000;padding:0.097cm;"| As we can see, as the name goes, the fill method fills the array with the given argument i.e 6 |
Line 214: | Line 211: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| We are going to copy all the elements of the array '''marks''' into the array '''marksCopy''' | | style="border:1pt solid #000000;padding:0.097cm;"| We are going to copy all the elements of the array '''marks''' into the array '''marksCopy''' | ||
− | + | So remove arrays dot fill | |
− | Type '''int marksCopy<nowiki>[];</nowiki>''' | + | And Type '''int marksCopy<nowiki>[];</nowiki>''' |
|- | |- | ||
Line 225: | Line 222: | ||
Change '''toString(marks)''' to '''toString(marksCopy)''' | Change '''toString(marks)''' to '''toString(marksCopy)''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Type '''marksCopy = copyOf(marks, 5);''' | + | | style="border:1pt solid #000000;padding:0.097cm;"|next line, |
+ | Type '''marksCopy = copyOf(marks, 5);''' | ||
− | + | This method takes two arguments. | |
− | The first argument is the array to copy | + | The first argument is the name of the array from which you want to copy the elements.i.e marks |
− | + | the second is the no.of elements to copy overhere we will copy 5. | |
− | + | Then in arrays dot strings changes marks to marks copy | |
− | + | ||
− | + | ||
Line 243: | Line 239: | ||
|- | |- | ||
| 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;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Now save and run the program |
We see that the elements of the array '''marks''' have been copied to the array '''marksCopy'''. | We see that the elements of the array '''marks''' have been copied to the array '''marksCopy'''. | ||
Line 255: | Line 251: | ||
− | '''Change 5''' to '''3''' | + | Let's'''Change 5''' to '''3'''. |
− | |||
|- | |- | ||
| 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;"| Save and Run | + | | style="border:1pt solid #000000;padding:0.097cm;"| Save and Run |
As we can see, only the first three elements have been copied. | As we can see, only the first three elements have been copied. | ||
Line 274: | Line 269: | ||
save and run. Point to output | save and run. Point to output | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Change '''3''' to '''8''' | + | | style="border:1pt solid #000000;padding:0.097cm;"|So Change '''3''' to '''8''' |
Line 287: | Line 282: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Change '''copyOf(marks, 8)''' to '''copyOfRange(marks, 1, 4)''' | | style="border:1pt solid #000000;padding:0.097cm;"| Change '''copyOf(marks, 8)''' to '''copyOfRange(marks, 1, 4)''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Change '''copyOf '''to''' copyOfRange '''and''' 8''' to '''1, 4''' | + | | style="border:1pt solid #000000;padding:0.097cm;"|So Change '''copyOf '''to''' copyOfRange '''and''' 8''' to '''1, 4''' |
+ | |||
+ | This methods copies all the elements starting from index 1 and stopping at index 3. | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 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;"| Save and Run | + | | style="border:1pt solid #000000;padding:0.097cm;"| Save and Run |
− | + | ||
− | As we can see, the elements from index 1 to | + | As we can see, the elements from index 1 to 3 have been copied. |
− | Note that we have given 1, 4 | + | Note that we have given 1, 4 as our arguments |
Line 309: | Line 302: | ||
− | Only the elements till index 3 have been copied. | + | Only the elements till index 3 have been copied. It stops one elements before the gven range |
|- | |- | ||
Line 318: | Line 311: | ||
(0, 4) implies from index 0 to index 3 | (0, 4) implies from index 0 to index 3 | ||
− | (4, 6) implies from index 4 to index | + | (4, 6) implies from index 4 to index 5 |
− | So it behaves as if (0, 4) + (4, 6) = (0, | + | So it behaves as if (0, 4) + (4, 6) = (0, 5) |
|- | |- | ||
Line 330: | Line 323: | ||
'''Summary''' | '''Summary''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"|This brings us o the end of this tutorial. |
In this tutorial we have learnt | In this tutorial we have learnt | ||
Line 336: | Line 329: | ||
how to '''import the class Arrays'''. | how to '''import the class Arrays'''. | ||
− | Perform array operations like ''' | + | Perform array operations like '''to strings''',sort fill. |
|- | |- |
Latest revision as of 11:05, 10 May 2013
Title of script: Array Operations
Author: TalentSprint
Keywords: array operations, java, sort, fill, copy, video tutorial
Visual Cue | Description |
Slide 1
Welcome |
Welcome to the spoken tutorial on Array Operations in java. |
Slide 2
Learning Outcomes |
In this tutorial, you will learn how to
import the class Arrays and, perform basic operations on arrays. |
Slide 3
Tools Used |
For this tutorial we are using
Ubuntu 11.10, JDK 1.6 and Eclipse 3.7.0 |
Slide 4
Prerequisites |
For this tutorial, you should have knowledge on arrays in Java.
|
Slide 5
import java.util.Arrays; |
The methods for array operations are available in a class called Arrays.
|
Point to the code
{Eclipse should contain the following code} public class ArraysDemo{ public static void main(String[] args){ } } |
Now Switch to eclipse.
|
Before the line public class..., type
import java.util.Arrays; |
The import statement is written before the class definition.
|
Inside the main function, type
int marks[] = {2, 7, 5, 4, 8}; |
Now let us add an array.
|
In next line, type
String mStr = Arrays.toString(marks);
|
Now we shall use a method available in the Arrays class to get a string representation of the array and print it.
Now this toString method gives a string representation of the array.
|
Save and run. Point to output | As we can see int the output, the toString method has given a string representation of the array.
|
Before the line Arrays.toString, type
Arrays.sort(marks);
|
So before the line Arrays dot toString within brackets marks type Arrays dot sort(marks);
|
Save and run. Point to output | As we can see in the output sort method has sorted to the array in the ascending order
|
Remove the line Arrays.sort and type
Arrays.fill(marks, 6); |
The fill method takes two arguments.
Remove the sorting line and
This is our first argument and second is the value that should be felt in the array we will give it 6 and semicolon. Save and Run |
Save and run. Point to output | As we can see, as the name goes, the fill method fills the array with the given argument i.e 6
|
Remove the Arrays.fill.. line and type
int marksCopy[]; |
We are going to copy all the elements of the array marks into the array marksCopy
So remove arrays dot fill And Type int marksCopy[]; |
In the next line, type
|
next line,
Type marksCopy = copyOf(marks, 5);
the second is the no.of elements to copy overhere we will copy 5. Then in arrays dot strings changes marks to marks copy
|
Save and run. Point to output | Now save and run the program
We see that the elements of the array marks have been copied to the array marksCopy.
|
Change copyOf(marks, 5) to copyOf(marks, 3) | Let us see what happens if we change the no.of elements to be copied.
|
Save and run. Point to output | Save and Run
As we can see, only the first three elements have been copied.
|
Change copyOf(marks, 3) to copyOf(marks, 8)
|
So Change 3 to 8
|
Change copyOf(marks, 8) to copyOfRange(marks, 1, 4) | So Change copyOf to copyOfRange and 8 to 1, 4
This methods copies all the elements starting from index 1 and stopping at index 3.
|
Save and run. Point to output. | Save and Run
As we can see, the elements from index 1 to 3 have been copied.
|
This behaviour ensures that continuity of ranges is maintained.
(4, 6) implies from index 4 to index 5
| |
Minimize the Eclipse window and switch to slides.
Summary |
This brings us o the end of this tutorial.
In this tutorial we have learnt how to import the class Arrays. Perform array operations like to strings,sort fill. |
Slide 7Assignment | The assignment for this tutorial is,
|
Slide 8About the Spoken Tutorial Project
|
To know more about the Spoken Tutorial project, watch the video available at the following link, that summarises the project.
If you do not have good bandwidth, you can download and watch it. |
Slide 9Spoken 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 10Acknowledgement
|
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 11About the contributor
|
This tutorial has been contributed by TalentSprint. Thanks for joining.
|