0% found this document useful (0 votes)
14 views

Class Note - PRF (Arrays)

The document discusses arrays in Java including how to declare, initialize, and access array elements. It provides examples of declaring arrays, initializing arrays with user input or random values, and calculating total, maximum and minimum values from array elements. The examples become more complex by adding additional operations like finding max/min values and printing the array and calculations.

Uploaded by

MUSIC & TECH
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)
14 views

Class Note - PRF (Arrays)

The document discusses arrays in Java including how to declare, initialize, and access array elements. It provides examples of declaring arrays, initializing arrays with user input or random values, and calculating total, maximum and minimum values from array elements. The examples become more complex by adding additional operations like finding max/min values and printing the array and calculations.

Uploaded by

MUSIC & TECH
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/ 35

CMJD | Diploma in Comprehensive Master Java Developer

Arrays

01. Issue

class Example{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int a,b,c,d,e,f,g,h,i,j;
for(int k =0; k < 10; k++){
System.out.print("Input Number : ");
a, b , c=input.nextInt(); // wrong
}
}
}

02. from 01 using Array.


import java.util.*;
class Example{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int[] ar = new int[10];
for(int k =0; k < 10; k++){
System.out.print("Input Number : ");
ar[k] = input.nextInt();
}
}
}

Page 1 of 35
CMJD | Diploma in Comprehensive Master Java Developer

03.
Exercise

import java.util.*;

class Example{

public static void main(String args[]){


Scanner input = new Scanner(System.in);
int[] marks = new int[10];

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


System.out.print("Input Marks for Student "+ (i+1)+ " : ");
marks[i] = input.nextInt();
}

// print marks ==> [45,23,67,45,...89]


}
}

04 from 03.

import java.util.*;

class Example{

public static void main(String args[]){


Scanner input = new Scanner(System.in);
int[] marks = new int[10];

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


System.out.print("Input Marks for Student "+ (i+1)+ " : ");
marks[i] = input.nextInt();
}
// print marks ==> [45,23,67,45,...89]
System.out.print("[");
for(int i = 0; i<10; i++){
System.out.print(marks[i]+ " , ");
}
System.out.println("\b\b]");
}
}

Page 2 of 35
CMJD | Diploma in Comprehensive Master Java Developer

05. Exercise (from 04 using Random numbers)

import java.util.*;

class Example{

public static void main(String args[]){


Random input = new Random();
int[] marks = new int[10];

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


marks[i] = input.nextInt(101);
}
// print marks ==> [45,23,67,45,...89]

System.out.print("[");
for(int i = 0; i<10; i++){
System.out.print(marks[i]+ " , ");
}

System.out.println("\b\b]");
}
}

06. Exercise

import java.util.*;

class Example{
public static void main(String args[]){
Random input = new Random();
int[] marks = new int[10];
for(int i =0; i < 10; i++){
marks[i] = input.nextInt(101);
}
//Find Total Marks
int total = 0;
// print marks ==> [45,23,67,45,...89]
System.out.print("[");
for(int i = 0; i<10; i++){
System.out.print(marks[i]+ " , ");
}
System.out.println("\b\b]");
System.out.println("Total : " + total);
}
}

Page 3 of 35
CMJD | Diploma in Comprehensive Master Java Developer

07 from 06.

import java.util.*;

class Example{
public static void main(String args[]){
Random input = new Random();
int[] marks = new int[10];

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


marks[i] = input.nextInt(101);
}

//Find Total Marks


int total = 0;
for(int i = 0; i< 10; i++){
total+=marks[i];
}

// print marks ==> [45,23,67,45,...89]

System.out.print("[");
for(int i = 0; i<10; i++){
System.out.print(marks[i]+ " , ");
}

System.out.println("\b\b]");

System.out.println("Total : " + total);


}
}

08.Exercise

import java.util.*;

class Example{

public static void main(String args[]){


Random input = new Random();
int[] marks = new int[10];

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


marks[i] = input.nextInt(101);
}

Page 4 of 35
CMJD | Diploma in Comprehensive Master Java Developer

//Find Total Marks


int total = 0;
for(int i = 0; i< 10; i++){
total+=marks[i];
}

//find max
int max = 0;

//find min
int min = 0;

// print marks ==> [45,23,67,45,...89]

System.out.print("[");
for(int i = 0; i<10; i++){
System.out.print(marks[i]+ " , ");
}

System.out.println("\b\b]");

System.out.println("Total : " + total);


System.out.println("Maximum : " + max);
System.out.println("Minimum : " + min);
}
}

09 from 08.

import java.util.*;

class Example{

public static void main(String args[]){


Random input = new Random();
int[] marks = new int[10];

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


marks[i] = input.nextInt(101);
}

//Find Total Marks


int total = 0;
for(int i = 0; i< 10; i++){
total+=marks[i];
}

Page 5 of 35
CMJD | Diploma in Comprehensive Master Java Developer

//find max
int max = 0;
for(int i =0; i < 10; i++){
if(marks[i]>max){
max = marks[i];
}
}

//find min
int min = marks[0];

for(int i = 1; i < 10; i++){


if(marks[i]<min){
min=marks[i];
}
}

// print marks ==> [45,23,67,45,...89]

System.out.print("[");
for(int i = 0; i<10; i++){
System.out.print(marks[i]+ " , ");
}

System.out.println("\b\b]");

System.out.println("Total : " + total);


System.out.println("Maximum : " + max);
System.out.println("Minimum : " + min);
}
}

10. Case 1

import java.util.*;
class Example{
public static void main(String args[]){
int x;
x=100;
System.out.println(x);
int[] xr;
xr = new int[5];
System.out.println(xr);
xr[1] = 100;
System.out.println(xr[1]);
}
}

Page 6 of 35
CMJD | Diploma in Comprehensive Master Java Developer

11 Case 2.

class Example{

public static void main(String args[]){


int[] xr; // Create Array Reference --> [Array Declaration]
xr = new int[5]; // Create Array Object --> [Array Construction]
xr[0] = 10; // [Array Initialization]
xr[1] = 20; // [Array Initialization]
}
}

12 Case 3.

class Example{

public static void main(String args[]){


int[] a; // Legal
int []b; // Legal
int c[]; // Legal
[]int d; // Illegal
int[5] e; // Illegal

}
}

Page 7 of 35
CMJD | Diploma in Comprehensive Master Java Developer

13 Case 4 (Default Values).

class Example{

public static void main(String args[]){


int x,y,z;
//System.out.println(x + " " + y + " " + z); // Illegal

int [] xr = new int[3];


System.out.println(xr[0] + " " + xr[1] + " " + xr[2]);
}
}

14 Exercise.

class Example{

public static void main(String args[]){


boolean [] xr = new boolean[3];
System.out.println(xr[0] + " " + xr[1] + " " + xr[2]); // prints false false false
}
}

Page 8 of 35
CMJD | Diploma in Comprehensive Master Java Developer

15 Exercise.

class Example{

public static void main(String args[]){


char [] xr = new char[3];
System.out.println(xr[0] + " " + xr[1] + " " + xr[2]);

System.out.println("ASCII : " + (int)xr[0]); //ASCII --> 0


}
}

16 Case 5.

class Example{

public static void main(String args[]){


// ----------------------Method 1 ------------------------
int [] ar = new int [3];
System.out.println(ar[0] + " " + ar[1] + " " + ar[2]); // 0 0 0

//int [] br = new int[];

// ----------------------Method 2 ------------------------
int [] cr = {10,20,30};
System.out.println(cr[0] + " " + cr[1] + " " + cr[2]); // 10 20 30

int [] dr;
//dr = {10,20,30}; // Illegal

// ----------------------Method 3 ------------------------

int [] er;
er = new int[]{10,20,30}; // Legal
}
}

17 Case 6 Length of Array.

class Example{

public static void main(String args[]){


int [] ar = new int [10];
System.out.println(ar.length);
}
}

Page 9 of 35
CMJD | Diploma in Comprehensive Master Java Developer

18 Exercise.

class Example{

public static void main(String args[]){


int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};

//print marks -- [ , , , , ]
}
}

19 from 18.

class Example{
public static void main(String args[]){
int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};
System.out.print("[");
for(int i = 0; i < marks.length; i++){
System.out.print(marks[i] + ", ");
}
System.out.print("\b\b]");
}
}

20 Case 7 (Last Element of an array).

class Example{
public static void main(String args[]){
int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};
System.out.println("Last Element : " + marks[marks.length-1]);
}
}

21 Case 8 Array index out of bound Exception.

class Example{

public static void main(String args[]){


int [] ar = {10,20,30};
System.out.println("ar[0] : " + ar[0]);
System.out.println("ar[1] : " + ar[1]);
System.out.println("ar[2] : " + ar[2]);
System.out.println("ar[3] : " + ar[3]); // Legal Statement, Runtime Error
}
}

Page 10 of 35
CMJD | Diploma in Comprehensive Master Java Developer

22 Exercise.

class Example{
public static void main(String args[]){
int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};

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


System.out.println(marks[i]);
}
}
}

23 from 22.

class Example{
public static void main(String args[]){
int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};
for(int i = 0; i < marks.length; i++){
System.out.println(marks[i]);
}
}
}

24 Case 9.

class Example{
public static void main(String args[]){
//int [] xr = new byte[5]; // Illegal
//byte [] br = new int[5]; // Illegal
int [] xr;
xr = new int[5];
}
}
25 Exercise.

class Example{
public static void main(String args[]){
int [] xr = new int[5];
double dr [] = new double[5];

dr[0] = xr[0]; //legal --> Wider Conversion


//xr[0] = dr[0]; // Illegal --> narrow
xr[0] = (int)dr[0]; // legal --> narrow casting

xr=dr// illegal
dr=xr; // illegal
}
}

Page 11 of 35
CMJD | Diploma in Comprehensive Master Java Developer

26 Case 10 (Class ‘Arrays’).

import java.util.*;

class Example{

public static void main(String args[]){


int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};

String stringMarksArray = Arrays.toString(marks);


System.out.println(stringMarksArray);

Arrays.sort(marks);
System.out.println(Arrays.toString(marks));

}
}

Page 12 of 35
CMJD | Diploma in Comprehensive Master Java Developer

Passing an array to a method

27 Step 1.

class Example{

public static void main(String args[]){


int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};
printArray();
}
}

28 Step 2.

class Example{

public static void printArray(){


System.out.print("[");
for(int i = 0; i < marks.length; i++){
System.out.print(marks[i]+", ");
}
System.out.println("\b\b]");
}
public static void main(String args[]){
int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};
printArray();
}
}

29 Step 3

class Example{

public static void printArray(){


System.out.print("[");
for(int i = 0; i < marks.length; i++){
System.out.print(marks[i]+", ");
}
System.out.println("\b\b]");
}
public static void main(String args[]){
int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};
printArray(marks);

}
}

Page 13 of 35
CMJD | Diploma in Comprehensive Master Java Developer

30 Step 4.

class Example{

public static void printArray(int[] marks){


System.out.print("[");
for(int i = 0; i < marks.length; i++){
System.out.print(marks[i]+", ");
}
System.out.println("\b\b]");
}

public static void main(String args[]){


int [] marks = {56,76,56,67,89,87,65,68,78,97,34,67,89,23,45};
printArray(marks);
}
}

31 Exercise.

class Example{

public static void increment(int a, int b, int c){


a++;
b++;
c++;
}

public static void main(String args[]){

int [] ar = {100,200,300};
System.out.println(ar[0] + " " + ar[1] + " " + ar[2]); // 100 200 300

increment(ar[0], ar[1], ar[2]); // Called by values


System.out.println(ar[0] + " " + ar[1] + " " + ar[2]); // 100 200 300

}
}

Page 14 of 35
CMJD | Diploma in Comprehensive Master Java Developer

32 Exercise.

import java.util.*;

class Example{

public static void increment(int[]a){


a[0]++;
a[1]++;
a[2]++;
}

public static void main(String args[]){


int [] ar = {100,200,300};
System.out.println(ar[0] + " " + ar[1] + " " + ar[2]); // 100 200 300

increment(ar); // Called by Reference


System.out.println(ar[0] + " " + ar[1] + " " + ar[2]); // 101 201 301

}
}

Page 15 of 35
CMJD | Diploma in Comprehensive Master Java Developer

33 Exercise.

import java.util.*;

class Example{

public static void main(String []args) {


Scanner input=new Scanner(System.in);
System.out.print("Input no of students : ");
final int N=input.nextInt();
//1. Create an array to store student's marks, named "marks", size
int[] marks=createAnArray(N);
//2. Read and store marks [Read random number 0 to 100]
readMarks(marks);
//3. Find Total
int total=findTotal(marks);
//4. Find max
int max=findMax(marks);
//5. Find min
int min=findMin(marks);

//6. Print marks [45, 23,.....]


printMarks(marks);

System.out.println("Total : "+total);
System.out.println("Maximum : "+max);
System.out.println("Minimum : "+min);
}
}

Page 16 of 35
CMJD | Diploma in Comprehensive Master Java Developer

34 from 33.
import java.util.*;

class Example{
public static int[] createAnArray(int length){
int [] arr = new int[length];
return arr;
}
public static void readMarks(int[] arr){
Random r = new Random();
for(int i = 0; i<arr.length; i++){
arr[i] = r.nextInt(101);
}
}
public static int findTotal(int[] arr){
int total = 0;
for(int i = 0; i< arr.length; i++){
total += arr[i];
}
return total;
}
public static int findMax(int[] arr){
int max = arr[0];
for(int i = 1; i < arr.length; i ++){
if(max < arr[i]){
max = arr[i];
}
}
return max;
}
public static int findMin(int[] arr){
int min = arr[0];
for(int i = 1; i < arr.length; i++){
if(min> arr[i]){
min = arr[i];
}
}

return min;
}
public static void printMarks(int[] arr){
System.out.print("[");
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i]+", ");
}
System.out.println("\b\b]");
}

Page 17 of 35
CMJD | Diploma in Comprehensive Master Java Developer

public static void main(String []args) {

Scanner input=new Scanner(System.in);


System.out.print("Input no of students : ");
final int N=input.nextInt();

//1. Create an array to store student's marks, named "marks", size


int[] marks=createAnArray(N);

//2. Read and store marks [Read random number 0 to 100]


readMarks(marks);

//3. Find Total


int total=findTotal(marks);

//4. Find max


int max=findMax(marks);

//5. Find min


int min=findMin(marks);

//6. Print marks [45, 23,.....]


printMarks(marks);

System.out.println("Total : "+total);
System.out.println("Maximum : "+max);
System.out.println("Minimum : "+min);

}
}

Page 18 of 35
CMJD | Diploma in Comprehensive Master Java Developer

For-each Loop

35.

class Example{
public static void main(String []args) {
int [] arr = {2,3,4,3,4,7,5,8,9,1,2,5,5};

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


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

System.out.println();

//--------- Using For Each ----------


for(int a: arr){
System.out.print(a + " ");
}

}
}

36 Case 1.

class Example{
public static void main(String []args) {
int [] arr = {2,3,4,3,4,7,5,8,9,1,2,5,5};
int a;
for(a: arr){ // Illegal
System.out.print(a + " ");
}

}
}

Page 19 of 35
CMJD | Diploma in Comprehensive Master Java Developer

37 Case 2.

class Example{

public static void main(String []args) {


int [] arr = {2,3,4,3,4,7,5,8,9,1,2,5,5};

for(int a: arr){
System.out.print(a + " ");
}
System.out.println(a);// Illegal
}
}

38 Case 3.

class Example{

public static void main(String []args) {


int [] arr = {2,3,4,3,4,7,5,8,9,1,2,5,5};

for(long a: arr){ // Legal


System.out.print(a + " ");
}

for(byte a: arr){ // Illegal


System.out.print(a + " ");
}
}
}

39 Case 4.
import java.util.*;

class Example{
public static void main(String []args) {
int [] arr = {10,20,30,40};
System.out.println(Arrays.toString(arr));

for(int a: arr){
a++;
System.out.println(a);
}
System.out.println(Arrays.toString(arr));

}
}

Page 20 of 35
CMJD | Diploma in Comprehensive Master Java Developer

Variable length argument (var-args)


Since JDK 1.5

40 Step 1.

class Example{
public static void main(String []args) {
printTotal(10,20);// 10+20 = 30
}
}

41 Step 2.

class Example{
public static void printTotal(int a, int b){
int tot = a+b;
System.out.println(a + " + " + b + " = " + tot);
}
public static void main(String []args) {
printTotal(10,20);// 10+20 = 30
}
}

42 Step 3.

class Example{

public static void printTotal(int a, int b){


int tot = a+b;
System.out.println(a + " + " + b + " = " + tot);
}
public static void main(String []args) {
printTotal(10,20);// 10+20 = 30
printTotal(10,20,30);// 10+20+30 = 60

}
}

Page 21 of 35
CMJD | Diploma in Comprehensive Master Java Developer

43 Step 4.

class Example{

public static void printTotal(int a, int b){


int tot = a+b;
System.out.println(a + " + " + b + " = " + tot);
}

public static void printTotal(int a, int b, int c){


int tot = a+b+c;
System.out.println(a + " + " + b + " + " + c + " = " + tot);
}

public static void main(String []args) {


printTotal(10,20);// 10+20 = 30
printTotal(10,20,30);// 10+20+30 = 60
}
}

44 Step 5 Wrong.

class Example{
public static void printTotal(int[] a){
int tot = 0;
for (int i : a) {
System.out.println(i + " + ");
tot+=i;
}
System.out.println("\b\b = " + tot);
}
public static void main(String []args) {
printTotal(10,20);// 10+20 = 30
printTotal(10,20,30);// 10+20+30 = 60
printTotal(10,20,30,40);// 10+20+30+40 = 100

}
}

Page 22 of 35
CMJD | Diploma in Comprehensive Master Java Developer

45 Step 6 (var-args).

class Example{

public static void printTotal(int... ar){ //--> int[] ar


int tot = 0;
for (int i : ar) {
System.out.print(i + " + ");
tot+=i;
}
System.out.println("\b\b= " + tot);
}

public static void main(String []args) {


printTotal(10,20);// 10+20 = 30
printTotal(10,20,30);// 10+20+30 = 60
printTotal(10,20,30,40);// 10+20+30+40 = 100
}
}

46.

class Example{

public static void printTotal(int... a){//--> int[] ar


int tot = 0;
for (int i : a) {
System.out.print(i + " + ");
tot+=i;
}
System.out.println("\b\b = " + tot);
}

public static void main(String []args) {


printTotal(new int[]{10,20,30});

}
}

Page 23 of 35
CMJD | Diploma in Comprehensive Master Java Developer

47.

class Example{

public static void printTotal(int... a){//--> int[] ar


int tot = 0;
for (int i : a) {
System.out.print(i + " + ");
tot+=i;
}
System.out.println("\b\b= " + tot);
}

public static void main(String []args) {


printTotal(10,20);
printTotal(10,20,30);
printTotal(10,20,30,40);
printTotal(new int[]{10,20,30,40});
}
}

class Example{

public static void printTotal(int[] a){//--> int[] ar


int tot = 0;
for (int i : a) {
System.out.print(i + " + ");
tot+=i;
}
System.out.println("\b\b= " + tot);
}

public static void main(String []args) {


//printTotal(10,20); // illegal
//printTotal(10,20,30); // illegal
//printTotal(10,20,30,40); // illegal
printTotal(new int[]{10,20,30,40}); // Legal
}
}

Page 24 of 35
CMJD | Diploma in Comprehensive Master Java Developer

48.

class Example{

public static void printTotal(int[] a){

public static void printTotal(int... a){ // Illegal

public static void main(String []args) {

}
}

49 Exercise 1.

import java.util.*;
class Example{

public static void main(String []args) {


int[] ar={34,90,24,87,12,67,97,73,61,19,69,28,79};
printArray(ar); //[34,90,24,87,12,67,97,73,61,19,69,28,79]
}
}

50 from Exercise 1.

class Example{

public static void printArray(int [] ar){


System.out.print("[");
for (int i : ar) {
System.out.print(i+", ");
}

System.out.println("\b\b]");
}

public static void main(String []args) {


int[] ar={34,90,24,87,12,67,97,73,61,19,69,28,79};
printArray(ar); //[34,90,24,87,12,67,97,73,61,19,69,28,79]
}
}

Page 25 of 35
CMJD | Diploma in Comprehensive Master Java Developer

51 Exercise 2.

class Example{

public static void main(String []args) {


int[] ar={31,34,56,78,98,76,5,43,23,45,67,75,12};
String s1=toString(ar);
System.out.println(s1);//[31,34,56,78,98,76,5,43,23,45,67,75,12]
}
}

52 from Exercise 2.
class Example{

public static String toString(int [] ar){


String s="[";
for (int i : ar) {
s+=i+ ", ";
}
s+= "\b\b]";

return s;
}

public static void main(String []args) {


int[] ar={31,34,56,78,98,76,5,43,23,45,67,75,12};
String s1=toString(ar);
System.out.println(s1);//[31,34,56,78,98,76,5,43,23,45,67,75,12]
}
}

53 Exercise 3.

class Example{
public static void main(String []args) {
int[] ar={31,34,56,78,98,76,5,43,23,45,67,75,12};
printReverse(ar); //[12,75,67..........34,31]

}
}

Page 26 of 35
CMJD | Diploma in Comprehensive Master Java Developer

54 from Exercise 3.

class Example{
public static void printReverse(int [] ar){
System.out.print("[");
for(int i = ar.length-1; i>=0; i--){
System.out.print(ar[i] + ", ");
}
System.out.println("\b\b]");
}
public static void main(String []args) {
int[] ar={31,34,56,78,98,76,5,43,23,45,67,75,12};
printReverse(ar); //[12,75,67..........34,31]
}
}

55 Exercise 4.

import java.util.*;
class Example{
public static void main(String []args) {
double[] salary={100000.0, 150000.0, 50000.0, 300000.0, 500000.0};
System.out.println(Arrays.toString(salary));//[100000.0, 150000.0, 50000.0,
300000.0, 500000.0]
incrementSalary(salary,20.0); //20-->20%(rate)
System.out.println(Arrays.toString(salary));//[]
}
}

56 from Exercise 4.

import java.util.*;
class Example{
public static void incrementSalary(double [] ar, double rate){
for(int i = 0; i< ar.length; i++){
ar[i]*=(rate+100)/100;
}
}
public static void main(String []args) {
double[] salary={100000.0, 150000.0, 50000.0, 300000.0, 500000.0};

System.out.println(Arrays.toString(salary));//[100000.0, 150000.0, 50000.0,


300000.0, 500000.0]
incrementSalary(salary,20.0); //20-->20%(rate)
System.out.println(Arrays.toString(salary));//[]
}
}

Page 27 of 35
CMJD | Diploma in Comprehensive Master Java Developer

57 Exercise 5.

import java.util.*;
class Example{
public static void main(String []args) {
int[] ar={100,200,300,400,500,600};
int[] br=new int[ar.length];

System.out.println(Arrays.toString(ar));//[100,200,300,400,500,600]
System.out.println(Arrays.toString(br));//[0, 0, 0, 0, 0, 0]
copyValuesFromTo(ar,br);
System.out.println(Arrays.toString(ar));//[100,200,300,400,500,600]
System.out.println(Arrays.toString(br));//[100,200,300,400,500,600]
}
}

58 from Exercise 5.

import java.util.*;
class Example{

public static void copyValuesFromTo(int [] ar, int [] br){


for(int i=0; i < ar.length ; i++){
br[i] = ar[i];
}
}
public static void main(String []args) {
int[] ar={100,200,300,400,500,600};
int[] br=new int[ar.length];

System.out.println(Arrays.toString(ar));//[100,200,300,400,500,600]
System.out.println(Arrays.toString(br));//[0, 0, 0, 0, 0, 0]
copyValuesFromTo(ar,br);
System.out.println(Arrays.toString(ar));//[100,200,300,400,500,600]
System.out.println(Arrays.toString(br));//[100,200,300,400,500,600]
}
}

Page 28 of 35
CMJD | Diploma in Comprehensive Master Java Developer

59 Exercise 6.

import java.util.*;
class Example{
public static void copyValuesFromTo(int [] ar, int [] br){
for(int i=0; i < ar.length ; i++){
br[i] = ar[i];
}
}

public static void main(String []args) {


int[] ar={100,200,300,400,500,600};
int[] br=new int[ar.length];
int[] cr=new int[ar.length-3];
int[] dr=new int[ar.length+3];

System.out.println(Arrays.toString(ar));//[100,200,300,400,500,600]

copyValuesFromTo(ar,br);
copyValuesFromTo(ar,cr);
copyValuesFromTo(ar,dr);

System.out.println(Arrays.toString(br));//[100,200,300,400,500,600]
System.out.println(Arrays.toString(cr));//[100,200,300]
System.out.println(Arrays.toString(dr));//[100,200,300,400,500,600,0,0,0]
}
}

60 from Exercise 6.
import java.util.*;
class Example{
public static void copyValuesFromTo(int [] ar, int [] br){
int minLenght = ar.length > br.length ? br.length : ar.length;

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


br[i] = ar[i];
}
}
public static void main(String []args) {
int[] ar={100,200,300,400,500,600};
int[] br=new int[ar.length];
int[] cr=new int[ar.length-3];
int[] dr=new int[ar.length+3];

System.out.println(Arrays.toString(ar));//[100,200,300,400,500,600]

copyValuesFromTo(ar,br);

Page 29 of 35
CMJD | Diploma in Comprehensive Master Java Developer

copyValuesFromTo(ar,cr);
copyValuesFromTo(ar,dr);

System.out.println(Arrays.toString(br));//[100,200,300,400,500,600]
System.out.println(Arrays.toString(cr));//[100,200,300]
System.out.println(Arrays.toString(dr));//[100,200,300,400,500,600,0,0,0]
}
}

61 Exercise 7.

import java.util.*;
class Example{

public static void main(String []args) {


int[] ar={100,200,300,400,500,600};
int[] br={10,20,30,40,50};
int[] cr;
cr=mergeTwoArrays(ar,br);

System.out.println(Arrays.toString(ar));//[100,200,300,400,500,600]
System.out.println(Arrays.toString(br));//[10,20,30,40,50]

System.out.println(Arrays.toString(cr));//[100,200,300,400,500,600,10,20,30,40,50]
}
}

62 from Exercise 7.

import java.util.*;
class Example{
public static int[] mergeTwoArrays(int[] a, int[] b){
int[] temp = new int[a.length + b.length];
for(int i= 0; i< a.length; i++){
temp[i]= a[i];
}
for(int i =0; i< b.length; i++){
temp[i+ a.length] = b[i];
}
return temp;
}
public static void main(String []args) {
int[] ar={100,200,300,400,500,600};
int[] br={10,20,30,40,50};
int[] cr;
cr=mergeTwoArrays(ar,br);
System.out.println(Arrays.toString(ar));//[100,200,300,400,500,600]

Page 30 of 35
CMJD | Diploma in Comprehensive Master Java Developer

System.out.println(Arrays.toString(br));//[10,20,30,40,50]
System.out.println(Arrays.toString(cr));//[100,200,300,400,500,600,10,20,30,40,50]
}
}

62 Exercise 8 (Array Searching).

import java.util.*;
class Example{
public static void main(String []args) {
int[] ar={67,45,12,32,43,54,65,76,78,91,20,38,48};
System.out.println(Arrays.toString(ar));

int index;
int key=32;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //3

key=91;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //9

key=99;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //-1

}
}

63 from Exercise 8.

import java.util.*;
class Example{

public static int search(int []ar, int key){


int index = -1;
for(int i = 0; i< ar.length; i++){
if(key == ar[i]){
index = i;
}
}

return index;
}
public static void main(String []args) {
int[] ar={67,45,12,32,43,54,65,76,78,91,20,38,48};

Page 31 of 35
CMJD | Diploma in Comprehensive Master Java Developer

System.out.println(Arrays.toString(ar));

int index;
int key=32;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //3

key=91;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //9

key=99;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //-1
}
}

64 from Exercise 8 (First Index) option 1.

import java.util.*;
class Example{
public static int search(int []ar, int key){
int index = -1;
for(int i = 0; i< ar.length; i++){
if(key == ar[i]){
index = i;
break;
}
}
return index;
}
public static void main(String []args) {
int[] ar={67,45,12,32,43,54,65,76,78,91,20,38,32};
System.out.println(Arrays.toString(ar));
int index;
int key=32;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //3
key=91;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //9
key=99;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //-1

}
}

Page 32 of 35
CMJD | Diploma in Comprehensive Master Java Developer

65 from Exercise 8(First Index) option 2.

import java.util.*;
class Example{
public static int search(int []ar, int key){
for(int i = 0; i< ar.length; i++){
if(key == ar[i]){
return i;
}
}
return -1;
}
public static void main(String []args) {
int[] ar={67,45,12,32,43,54,65,76,78,91,20,38,32};
System.out.println(Arrays.toString(ar));

int index;
int key=32;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //3

key=91;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //9

key=99;
index=search(ar,key);
System.out.println("Index of "+key+" : "+index); //-1

}
}
66 Exercise 9.

class Example{
public static void main(String []args) {
int[] ar={10,20,30,40,50};
int[] br={10,20,30,40,50,60};
int[] cr={10,20,30,40};
int[] dr={40,10,50,20,30};
int[] er={10,20,30,40,50};

System.out.println("ar==br : "+equals(ar,br)); //false


System.out.println("ar==cr : "+equals(ar,cr)); //false
System.out.println("ar==dr : "+equals(ar,dr)); //false
System.out.println("ar==er : "+equals(ar,er)); //true
}
}

Page 33 of 35
CMJD | Diploma in Comprehensive Master Java Developer

67 from Exercise 9.

import java.util.*;
class Example{
public static boolean equals(int[] a, int[] b){
boolean isEquals = true;
if(a.length != b.length){
isEquals = false;
} else{
for(int i = 0; i< a.length; i++){
if(a[i]!=b[i]){
isEquals = false;
break;
}
}
}

return isEquals;
}

public static void main(String []args) {


int[] ar={10,20,30,40,50};
int[] br={10,20,30,40,50,60};
int[] cr={10,20,30,40};
int[] dr={40,10,50,20,30};
int[] er={10,20,30,40,50};

System.out.println("ar==br : "+equals(ar,br)); //false


System.out.println("ar==cr : "+equals(ar,cr)); //false
System.out.println("ar==dr : "+equals(ar,dr)); //false
System.out.println("ar==er : "+equals(ar,er)); //true
}
}

68 Exercise 10.
import java.util.*;
class Example{
public static void main(String []args) {
int[] ar={34,65,87,19,94,72,83,47,89};
System.out.println(Arrays.toString(ar));//[34,65,87,19,94,72,83,47,89]
reverse(ar);
System.out.println(Arrays.toString(ar));//[89,47,83,72,94,19,87,65,34]
}
}

Page 34 of 35
CMJD | Diploma in Comprehensive Master Java Developer

69 for Exercise 10.


class Example{

public static void reverse(int[] ar){


for(int i =0, j= ar.length-1; i < j ; i++, j--){
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}

public static void main(String []args) {


int[] ar={34,65,87,19,94,72,83,47,89};
System.out.println(Arrays.toString(ar));//[34,65,87,19,94,72,83,47,89]
reverse(ar);
System.out.println(Arrays.toString(ar));//[89,47,83,72,94,19,87,65,34]
}
}

Page 35 of 35

You might also like