Difference between revisions of "Java/C2/Array-Operations/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 196: Line 196:
  
  
Type '''Arrays '''''dot '''''fill '''''within brackets''''' marks, 6.'''
+
Type '''Arrays '''''dot '''''fill '''''within brackets''''' marks, 6;'''
  
  

Revision as of 19:14, 23 January 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.


If not, for relevant tutorial please visit our website which is as shown http://spoken-tutorial.org

Slide 5

import java.util.Arrays

The methods for array operations are available in a class called Arrays.


To access them, we need to import that class.


It is done by the statement import java.util.Arrays.


We can access a method from the class.


We do it by adding a dot and the method name.


So Arrays dot toString means toString method from the class Arrays.

Point to the code


Minimize Slides and open Eclipse

{Eclipse should contain the following code}

public class ArraysDemo{

public static void main(String[] args){

}

}

Switch to eclipse.


We have already created a class ArraysDemo.


Let us now import the class Arrays.

Before the line public class..., type

import java.util.Arrays;

The import statement is given before the class definition.


Before public class, type


import java.util.Arrays


This statement says that java contains a package called util which contains the class Arrays and it has to be imported.

Inside the main function, type

int marks[] = {2, 7, 5, 4, 8};

Now let us add an array.


Inside the main function,type


int marks open close square brackets equal to within brackets 2, 7, 5, 4, 8 semicolon.

In next line, type

String mStr = Arrays.toString(marks);


System.out.println(mStr);

Now we shall use a method available in the Arrays class to get a string representation of the array and print it.


Type String mStr equal to Arrays dot toString within brackets marks semicolon.


The toString method gives a string representation of the array.


Now, we shall print the marks.


So, type System dot out dot println within brackets mStr semicolon.


Now let us run it.

Save and run. Point to output As we can see, the toString method has given a string representation of the array.


Now let us look at sorting the elements of the array.

Before the line Arrays.toString, type

Arrays.sort(marks);


Highlight sort.

So before the line Arrays dot toString within brackets marks type Arrays dot sort(marks);


The sort method in the Arrays class, sorts the elements of the array passed to it.


Now we are sorting the array and then printing the string form of it.


Let us look at the output.

Save and run. Point to output As we can see, the sort method has sorted the array.


Note that the sort method has changed the array itself.


This type of sorting is called inplace sorting.


It means the array which contains the elements is changed as a result of sorting.


The next method we are going to look at, is fill

Remove the line Arrays.sort and type

Arrays.fill(marks, 6);

The fill method takes two arguments.

The first is the array to fill and the second is what should it be filled with.


Remove the sorting line and


Type Arrays dot fill within brackets marks, 6;


Now let us look at the output.

Save and run. Point to output As we can see, as the name goes, the fill method fills the array with the given argument.


The next method we are going to look at, is copyOf

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


Type int marksCopy[];

In the next line, type


marksCopy = copyOf(marks, 5);


Change toString(marks) to toString(marksCopy)

Type marksCopy = copyOf(marks, 5);


The copyOf method takes two arguments.


The first argument is the array to copy from.

And the second is the no.of elements to copy.


Now let us look at the contents of the new array.



Save and run. Point to output 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.


Change 5 to 3


Let us now look at the new array.

Save and run. Point to output Save and Run the program

As we can see, only the first three elements have been copied.


Let us see what happens if the no.of elements to be copied is greater than the total no.of elements in the array.

Change copyOf(marks, 3) to copyOf(marks, 8)


save and run. Point to output

Change 3 to 8


Save and Run the program


As we can see, the extra elements have been set to the default value, which is 0.


Next we'll see how to copy a range of values.

Change copyOf(marks, 8) to copyOfRange(marks, 1, 4) Change copyOf to copyOfRange and 8 to 1, 4


copyOfRange(marks, 1, 4) copies all the elements starting from index 1 and stopping at index 3.


Now let us look at the output.

Save and run. Point to output. Save and Run the program


As we can see, the elements from index 1 to index 3 have been copied.


Note that we have given 1, 4 in the method.


But even, then the element at index 4 has not been copied.


Only the elements till index 3 have been copied.

This behaviour ensures that continuity of ranges is maintained.


(0, 4) implies from index 0 to index 3

(4, 6) implies from index 4 to index 6


So it behaves as if (0, 4) + (4, 6) = (0, 6)

Minimize the Eclipse window and switch to slides.


Slide 6

Summary

We have come to the end of this tutorial.

In this tutorial we have learnt

how to import the class Arrays.

Perform array operations like sort and copy.

Slide 7Assignment The assignment for this tutorial is,


Read about the Arrays.equals method and find out what is does.

Slide 8About the Spoken Tutorial Project
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


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
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


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
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


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
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint. Thanks for joining.



Contributors and Content Editors

Arya Ratish, Pratham920, Sneha