In this post i will explain about the Arrays class in Java which is inbuilt and we can use many predefined methods on this Array class which makes our problem simple.
1)sort():This method is used to sort the array elements.
2)equal():This is a method for comparing two arrays.
3)binarySearch():This is a method to find an element using binary search.This method returns the location of the element.
4)fill():This is a method to assign specified value to each element of specified array.
Arrays.fill(arr,7);
where arr is an array of type integer,then it will replace all the elements in the array with 7.
5)arrayCopy():This is a method by which we can copy elements of one array to another.
One thing to note here is that in method,
System.arraycopy(srcArray,startIndex,dstArray,startIndex,length);
if we provide length which is larger than srcArray then we will get an ArrayIndexOutOfBoundsException.
So above is all about Arrays class in Java.If there is any query let me know by comments or by email.
Thanks.
1)sort():This method is used to sort the array elements.
2)equal():This is a method for comparing two arrays.
3)binarySearch():This is a method to find an element using binary search.This method returns the location of the element.
4)fill():This is a method to assign specified value to each element of specified array.
Arrays.fill(arr,7);
where arr is an array of type integer,then it will replace all the elements in the array with 7.
5)arrayCopy():This is a method by which we can copy elements of one array to another.
Program Code:
package examples;
import java.util.Arrays;
public class ArraysClass {
public static void main(String[] args) {
int[] arr={2,5,3,6,8,65,23,45};
System.out.println("Elements of arr are ");
for(int element:arr){
System.out.println(element);
}
Arrays.sort(arr);
System.out.println("Sorted array is ");
for(int element:arr){
System.out.println(element);
}
int[] arr1=new int[6];
Arrays.fill(arr1, 7);
System.out.println("Elements of array after insertion are ");
for(int element:arr1){
System.out.println(element);
}
int [] arr2={1,4,6,8,9};
System.out.println("Elements of arr1 are ");
for(int element:arr2){
System.out.println(element);
}
System.arraycopy(arr2, 0, arr1, 0, arr2.length);
System.out.println("Elements of arr1 after copy ");
for(int element:arr1){
System.out.println(element);
}
int loc=Arrays.binarySearch(arr2, 3);
if(loc>=0){
System.out.println("Element 3 is found in arr2 at location "+(loc+1));
}else{
System.out.println("Element 3 is not present in the arr2");
}
loc=Arrays.binarySearch(arr2, 4);
if(loc>=0){
System.out.println("Element 4 is found in arr2 at location "+(loc+1));
}else{
System.out.println("Element 4 is not present in the arr2");
}
boolean equ=Arrays.equals(arr, arr1);
if(equ){
System.out.println("Both array arr and arr1 are same");
}else{
System.out.println("Arrays arr and arr1 are not same");
}
}
}
Output:
Elements of arr are
2
5
3
6
8
65
23
45
Sorted array is
2
3
5
6
8
23
45
65
Elements of array after insertion are
7
7
7
7
7
7
Elements of arr1 are
1
4
6
8
9
Elements of arr1 after copy
1
4
6
8
9
7
Element 3 is not present in the arr2
Element 4 is found in arr2 at location 2
Arrays arr and arr1 are not same
package examples;
import java.util.Arrays;
public class ArraysClass {
public static void main(String[] args) {
int[] arr={2,5,3,6,8,65,23,45};
System.out.println("Elements of arr are ");
for(int element:arr){
System.out.println(element);
}
Arrays.sort(arr);
System.out.println("Sorted array is ");
for(int element:arr){
System.out.println(element);
}
int[] arr1=new int[6];
Arrays.fill(arr1, 7);
System.out.println("Elements of array after insertion are ");
for(int element:arr1){
System.out.println(element);
}
int [] arr2={1,4,6,8,9};
System.out.println("Elements of arr1 are ");
for(int element:arr2){
System.out.println(element);
}
System.arraycopy(arr2, 0, arr1, 0, arr2.length);
System.out.println("Elements of arr1 after copy ");
for(int element:arr1){
System.out.println(element);
}
int loc=Arrays.binarySearch(arr2, 3);
if(loc>=0){
System.out.println("Element 3 is found in arr2 at location "+(loc+1));
}else{
System.out.println("Element 3 is not present in the arr2");
}
loc=Arrays.binarySearch(arr2, 4);
if(loc>=0){
System.out.println("Element 4 is found in arr2 at location "+(loc+1));
}else{
System.out.println("Element 4 is not present in the arr2");
}
boolean equ=Arrays.equals(arr, arr1);
if(equ){
System.out.println("Both array arr and arr1 are same");
}else{
System.out.println("Arrays arr and arr1 are not same");
}
}
}
Output:
Elements of arr are
2
5
3
6
8
65
23
45
Sorted array is
2
3
5
6
8
23
45
65
Elements of array after insertion are
7
7
7
7
7
7
Elements of arr1 are
1
4
6
8
9
Elements of arr1 after copy
1
4
6
8
9
7
Element 3 is not present in the arr2
Element 4 is found in arr2 at location 2
Arrays arr and arr1 are not same
One thing to note here is that in method,
System.arraycopy(srcArray,startIndex,dstArray,startIndex,length);
if we provide length which is larger than srcArray then we will get an ArrayIndexOutOfBoundsException.
So above is all about Arrays class in Java.If there is any query let me know by comments or by email.
Thanks.
0 comments:
Post a Comment