Arrays Class: Learn how to handle (copy, sort, search and manipulate) Arrays

One of the great advantages of programming in Java is that it provides us a tremendous amount of APIs. We mean, Java comes with many classes and methods ready to be used.

In this tutorial in our online and free Java course will learn to use the Arrays class and its main methods, ordering, comparing, look, put elements in arrays / vectors of utilities among other series.

How to use Arrays Class in Java

To import, place on top of your code:
import java.util.Arrays;

Unlike other classes, we will not directly declare objects: Arrays object_name;
In fact, when we create an Array, we can automatically use the methods of class Arrays in that vector (array).

In the following example, we show how to use the methods of class Arrays.

Sorting and searching an element in an Array in Java

In this Java example code for this tutorial, we will use the method 'sort', to ordering and method 'binarySearch', which does a search for a particular element and returns its location in the array.

Initially we define an Array of any integer.
To display the elements of an Array in Java, as a string, use the method:
Arrays.toString (array);
that takes an array as an argument.

Then we use the method 'sort', which orders the array in ascending order. That is, will change the position of the elements so that they are arranged from smallest to largest.
To use the sort method in Java do:
Arrays.sort (array);

We then show the array again, but as you string to see how the organization after the array sort method has come into action.

Then we store an integer, 'position', the position of the element '2112 'in the array:
Arrays.binarySearch(array, numberWeAreLookingFor);

IMPORTANT: This method only works if the array is in ascending order! That is, only use the 'binarySearch' after using the 'sort' because Binary Search is a kind of intelligent search (it does not come out looking just the elements one by one, its algorithm is based on sorting).

import java.util.Arrays;

public class arraysClass{
    public static void main(String[] args){
            int[] numbers={1, 4, 0, -13, 2112, 14, 17};
            int position;
            System.out.println("Array elements: "+ Arrays.toString(numbers));

            System.out.println("Sorted array: "+Arrays.toString(numbers));

            position=Arrays.binarySearch(numbers, 2112);
            System.out.println("Element '2112'position in array: "+ position);


Class Arrays methods

Depending on the methods we can use integer, float, double, char, short, List, etc. Object.
To know exactly if there is a method to apply in that you want to use, look at the documentation in the Arrays class:

Serves to find a specific element within the array. Returns the position in the array (integer).
If pass an array as an argument and a value, the search is made throughout the array.
We can also pass in a range of search. For example, to search for the element 'x' in the Array 'vector', from the element 'begin' element to the 'end' elment, do:
Arrays.binarySearch (vector, begin, end, x);

This method copies an array and returns another. The returns is a copy of the first.
If you receive two arguments - an array and a value, the array that your pass is the one you want to copy and the value is the number of elements you want to copy (or the number of elements you want your new array has. If you want have a larger array, this method fills with 0 or nulls):
newArray[] = Arrays.copyOf (originalArray, numberOfElementsToBeCopied);
You can also specify a range of values​​:
newArray[] = Arrays.copyOf (originalArray, begin, end);

Receives two arrays. Returns true if they are equal and false otherwise.

This melhod will fill the array with with a given value.
If you want all elements of 'array' has the value 'value':
Arrays.fill (array, value);
To fill only certain range of values​​:
Arrays.fill (array, begin, end);

Sorts the elements in ascending order:
Arrays.sort (array);
To sort a range of values​​:
Arrays.sort (array, begin, end);

Returns all elements of an array as a string:
Arrays.toString (array);

No comments: