Wednesday, October 23, 2013

Array Manipulation

In this post, you learn to do manipulations on the Array data structure. Array is a basic data structure that can perhaps be found in all programming languages. It generally is used to store homogeneous values. Searching and sorting are actions that are commonly performed on the array. You can write your own code to sort, search, copy the array or compare the array with another array. However, it wastes your time and the performances of the actions or operations might not good.

The Arrays class is utility that is shipped with the Java standard API. It can be used to sort, search, copy or compare arrays easily. To sort the array, you can use the sort method. This method will arrange the elements of the array in ascending order. If you want to sort the array in descending order, you need to implement the Comparator interface. The post about Collections class has an example of implementing the Comparator interface in list sorting operations. The sort method of the Arrays class accepts one argument which is the array that you want to sort.

import java.util.Arrays;
public class ArrayManipulation {
   public static void main(String[] args){


      //create an integer array
      Integer[] nums={12,43,45,78,56,10,35,89};
      //sort the array in ascending order
      Arrays.sort(nums);
      for(int i:nums){
           System.out.println(i);
           }


     }
}



You will use the binarySearch method to search an element of the array. It returns the index of the element if it is found in the array. Otherwise, it returns a negative value. The binary search algorithm is proved to be the fastest searching technique for the array. Before using the binarySearch method to search for an element of the array, you have to sort the array. This method accepts two arguments. One is the array object and another one is the element that you want to search for.

//search an element of the array
int pos=Arrays.binarySearch(nums,56);
if(pos>0)
    System.out.println("Found at "+pos);
else{
    System.out.println("Not found"+pos);
}


The Arrays class has copyOf and copyOfRange methods that you can use to copy elements of an array to another array. You will use the copyOf method to copy a number of elements of the array by starting from the first element. This method accepts two arguments. One is the array object and another one is the number of elements to be copies from the original array. You will get NullPointerException error if the number of elements to be copies is greater than the size of the original array. If you want to copy the elements by starting from a specified index rather than from the beginning or first element of the array, the copyOfRange method can help you. This method accepts three arguments. The first argument is the array object. The second argument is the start or from index and the last argument is the end or to index. The start index must be between 0 and size or length of the array. The end or to index must be greater than or equal the start index. If the end index is greater than the length of the array, you will get NullPointerException error. The start index is inclusive while end index is exclusive.

//copy the first three elements from nums to numsCopy
Integer[] numsCopy=Arrays.copyOf(nums, 3);
for(int i:numsCopy){
    System.out.println(i);
}
//copy all elements from nums to newnums
Integer[] newnums=Arrays.copyOf(nums,nums.length);
for(int i:newnums){
    System.out.println(i);
}
//copy a specified range of elements from nums
Integer[] arange=Arrays.copyOfRange(nums, 2, 7);
for(int i:arange){
   System.out.println(i);
}


The Arrays class also provides a method that you can use to compare two arrays for equality. This method is called equals. The equals method accepts two array arguments. It returns true if the two arrrays are equal. Otherwise, it returns false.

Integer[] nums={12,43,45,78,56,10,35,89};
//copy all elements from nums to newnums
Integer[] newnums=Arrays.copyOf(nums,nums.length);
//check for equality
boolean b=Arrays.equals(nums, newnums);
if(b)
    System.out.println("The two arrays are equal.");
else
    System.out.println("The two arrays are not equal.");


2 comments:

  1. Media One has been the Full service digital marketing agency in the Singapore region. The agency would ensure to cater to your complete digital marketing needs using their experience and expertise in the best manner possible. They would ensure that you get good results using the services.

    ReplyDelete