-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltInMethods.java
More file actions
49 lines (47 loc) · 1.68 KB
/
builtInMethods.java
File metadata and controls
49 lines (47 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Using built in methods to sort & copy the array elements
package arrays;
import java.util.Arrays;
import java.util.Scanner;
public class builtInMethods {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements: ");
int n=sc.nextInt();
int[] arr=new int[n];
System.out.println("Enter the elements: ");
for(int i=0; i<n; i++){
arr[i]=sc.nextInt();
}
// Sorting the array (in ascending order)
Arrays.sort(arr); //***VVI
System.out.println("New sorted array is: ");
for(int ele : arr){
System.out.print(ele+" ");
}
System.out.println();
// copy the elements of an array in a new array
// int [] brr = arr; // SHALLOW COPY: Asole kono new array toiri e hoi ni. brr sudhu arr er e arek naam. Mane jodi brr er kono value change kora hoi tahole arr eu seta change hoye jabe.
// DEEP COPY: notun array toiri kore sekhane element gulo copy kore store kora.
int [] brr=Arrays.copyOf(arr,arr.length); //***VVI
brr[0]=70; // changing the 0th element
System.out.println("brr:");
for(int ele : brr){
System.out.print(ele+" ");
}
System.out.println();
System.out.println("arr: ");
for(int ele : arr){
System.out.print(ele+" ");
}
System.out.println();
//Method 2 of DEEP COPY: (Without using built in methods)
int [] crr= new int[arr.length];
for(int i=0; i<arr.length; i++){
crr[i]=arr[i];
}
System.out.println("crr: ");
for(int ele : crr){
System.out.print(ele+" ");
}
}
}