0% found this document useful (0 votes)
6 views7 pages

Lesson Plan - Arrays 2 PDF

The document covers various concepts related to arrays in Java, including taking input, array references, cloning, and copying arrays. It provides code examples for each concept, demonstrating how to manipulate arrays and solve basic problems such as finding occurrences of elements and checking if an array is sorted. The document concludes with a teaser for upcoming classes focused on problem-solving in arrays.

Uploaded by

0136Anirban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Lesson Plan - Arrays 2 PDF

The document covers various concepts related to arrays in Java, including taking input, array references, cloning, and copying arrays. It provides code examples for each concept, demonstrating how to manipulate arrays and solve basic problems such as finding occurrences of elements and checking if an array is sorted. The document concludes with a teaser for upcoming classes focused on problem-solving in arrays.

Uploaded by

0136Anirban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lesson:

Arrays - 2
Pre-Requisites
Basics of arrays and methods

List of Concepts Involved


Taking Array Input in Jav
Array Reference in Jav
Cloning an Array in Jav
Array Copy in Jav
Basic problems in Arrays

Topic: Taking input in an array


We can take input from the user by simply creating a Scanner class. Here, we create an array and take input
element by element using loops. Let us code for the same.

Code:

import java.io.*;

import java.util.*;

public class Main

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int[] arr = new int[n];

for(int i=0; i<n; i++) {

arr[i] = sc.nextInt();

System.out.println("\nArray elements are: ");

for (int i=0; i<n; i++) {

System.out.println(arr[i]);

Topic: Array Reference in Java


This is a very interesting topic of discussion. Do you know, when we use an “=” (equal to operator) to copy the
elements from one array to another, we actually assign a reference to the original array. The values in the new
array would be the same as that of the original array and if we make any changes in the new array it will be
reflected in the original array as well since both the new array and the original array refer to the same location.

We will illustrate that with the help of the example below.

Cracking the Coding Interview in JAVA - Foundation


Code:

public class Main

public static void main(String[] args) {

int a[] = { 1, 4, 7, 9 };

int n = a.length;

int b[] = new int[n];

b = a;

b[0] = 5;

System.out.println("Original array ");

for (int i = 0; i < n; i++)

System.out.print(a[i] + " ");

System.out.println("\nReferenced Array ");

for (int i = 0; i < n; i++)

System.out.print(b[i] + " ");

Output:

Original array

5 4 7 9

Referenced Array

5 4 7 9

Note: Changes in the referenced array will be reflected in the original array as well because both are referring
to the same location.

Topic: Cloning an array


Cloning of an array is a method to create another array with the same element values as the source array. A
deep copy of the original array is created in this with the new clone array having the same element values.

Code:

public class Main

public static void main(String[] args) {

int a[] = { 1, 4, 7, 9 };

int n = a.length;

int b[] = a.clone();

b[0] = 5;

System.out.println("Original array ");

for (int i = 0; i < n; i++)

System.out.print(a[i] + " ");

System.out.println("\nCloned Array ");

for (int i = 0; i < b.length; i++)

System.out.print(b[i] + " ");

Cracking the Coding Interview in JAVA - Foundation


Output:

Original array

1 4 7 9

Cloned Array

5 4 7 9

Note: When we make a copy of an entire array without referencing it, it forms a deep copy of that array. In case
of a cloned array, the changes will not be reflected in the original array as we have created a deep copy of the
original array.

Topic: Array Copy in Java


We can create a copy of an array using copyOf function in Java. We can use this function to copy values of the
whole array or a prefix part of the array.

Syntax:

int new_array[] = Arrays.copyOf (original_array, new_array_length);

Code:

import java.io.*;

import java.util.*;

public class Main

public static void main(String[] args) {

int a[] = { 1, 4, 7, 9 };

int n = a.length;

int b[] = Arrays.copyOf(a, 4);

b[0] = 5;

System.out.println("Original array ");

for (int i = 0; i < n; i++)

System.out.print(a[i] + " ");

System.out.println("\nCopied Array ");

for (int i = 0; i < b.length; i++)

System.out.print(b[i] + " ");

Output:

Original array

1 4 7 9

Copied Array

5479

Topic: Basic problems based on arrays


Example 1: Find the last occurrence of an element x in a given array.

Input a[] = {1 , 4, 7 , 9 , 1}

x = 1

Output 4

Cracking the Coding Interview in JAVA - Foundation


Code:

import java.io.*;

import java.util.*;

public class Main

public static int lastOccurance(int a[],

int x) {

int index = -1;

for(int i = 0; i < a.length; i++) {

if(a[i] == x)

index = i;

return index;

public static void main(String[] args) {

int a[] = { 1, 4, 7, 9 , 1};

System.out.println(lastOccurance(a,1));

Explanation: Traverse through the whole array and compare the current element with the target element ‘x’
and if it matches then it will be our last seen index.

Example 2: Count the number of occurrences of a particular element x.

Input a[] = {1 , 4, 7 , 9 , 1}

x = 1

Output 2

Code:

import java.util.*;

public class Main

public static int countOfElements(int

a[], int x){

int count = 0;

for(int i = 0; i < a.length; i++) {

if(a[i] == x)

count++;

return count;

public static void main(String[] args) {

int a[] = { 1, 4, 7, 9 , 1};

System.out.println(countOfElements(a,1));

Explanation: Check if the element is equal to the element x and increment the count variable and in the end
return it.

Cracking the Coding Interview in JAVA - Foundation


Example 3: Count the number of elements strictly greater than value x.

Input a[] = {1 , 4, 7 , 9 , 1}

x = 1

Output 3

Code :

import java.io.*;

import java.util.*;

public class Main

public static int countOfElements(int

a[], int x){

int count = 0;

for(int i = 0; i < a.length; i++) {

if(a[i] > x)

count++;

return count;

public static void main(String[] args) {

int a[] = { 1, 4, 7, 9 , 1};

System.out.println(countOfElements(a,1));

Explanation: Traverse the array and check if the element is greater than x and increment the count variable, in
the end simply return it.

Example 4: Check if the given array is sorted or not.

Input a[] = {1 , 2, 3 , 4 , 5}

Output True

Code :

import java.io.*;

import java.util.*;

public class Main

public static boolean check(int a[]) {

boolean ans = true;

for(int i = 1; i < a.length; i++) {

if(a[i] < a[i-1])

ans = false;

return ans;

public static void main(String[] args) {

int a[] = { 1, 2, 3, 4 , 5};

System.out.println(check(a));

Cracking the Coding Interview in JAVA - Foundation


Explanation: Assume the answer to be true, then traverse the array. At each point, check if the previous
element is greater than the current. The answer will become false if the array will not be sorted.

That is all for this class ! See you in the next one !

Till then, happy learning !

Upcoming Class Teasers:


Problem solving in arrays

Cracking the Coding Interview in JAVA - Foundation

You might also like