Arrays

Removing Duplicates Array Java

Removing Duplicates Array Java Summary: Java is so developed by Designers to write code easily for Developers. Forget the laborious days of C/C++ coding. This tutorial "Removing Duplicates Array Java" is the place to prove it. It is a general and interesting code for beginners and this can be achieved in many ways in Java. …

Removing Duplicates Array Java Read More »

Sort part array elements sort()

On sorting of array elements, three important and often used methods exist with Array class. They are discussed in this tutorial "Sort part array elements sort()" in easy terms, example and screenshot. Sorting array elements with sort() Sorting array elements with sort() and Comparator Sorting few array elements with sort() After seeing the first two …

Sort part array elements sort() Read More »

Java Shuffle Array shuffle()

Java collections framework comes with two classes Collections and Arrays (from java.util) to operate on the elements of collection classes (data structures) and arrays. To shuffle the elements of a data structure, the Collections class includes a method shuffle() but Arrays class does not come with one. To shuffle the elements of an array, we …

Java Shuffle Array shuffle() Read More »

Sorting complete array elements sort()

Arrays.sort() method sorts all the array elements in ascending order. It avoids writing tedious sorting algorithms. Two methods exist with Array class for sorting. Following are the method signatures. static void sort(int array1[]): Sorts all the elements of the array array1 in ascending numerical order. The sorting algorithm used is Quicksort (Mergesort) with Collections.sort()). The …

Sorting complete array elements sort() Read More »

Sorting array elements sort() Comparator

The earlier sort() example sorts implicitly in natural ascending order. The order can be customized with the overloaded sort() method that takes a Comparator object as parameter. Two methods exist for sorting array elements with Arrays class. Following are the method signatures. static void sort(int array1[]): Sorts all the elements of the array array1 in …

Sorting array elements sort() Comparator Read More »

Compare arrays equality equals()

Arrays.equals() method compares two arrays and tests whether they have the same elements are not. Returns a boolean value of true if they come with the same elements else false. Here, both arrays should have the same elements in the same order. If the order changes, the method returns false (even though they contain same …

Compare arrays equality equals() Read More »

Convert array to List with asList()

Arrays.asList() converts all the elements of the array into a List. It is the easiest way to pass the elements of an array to a List; an interoperability feature between arrays and collections framework classes. Convert array to List with asList() Following is the method signature static List asList(T array): It is useful to convert …

Convert array to List with asList() Read More »

Get hashcode with hashCode()

About Hashcode Generally, a programmer writes a lengthy for loop to compare two strings for their equality, checking each character in both the strings. It is a time consuming process and performance goes down especially when the strings are lengthy. To overcome this, Java designers introduced hash code. It is an integer number that can …

Get hashcode with hashCode() Read More »

Search array element binarySearch()

Arrays.binarySearch() method is useful to find out the index number of an element in the array. Two methods exist in the Arrays class for the purpose and following are their method signatures static int binarySearch(int array1[], int searchNum): Searches the element searchNum in the array array1. This method should be used in combination with sort() …

Search array element binarySearch() Read More »

Printing array elements with toString()

Arrays.toString() method makes it easier to print the elements of an array, forgetting the tedious iteration with for loops. Following is the method signature static String toString(int array1[]): Returns the array in string form useful to be displayed in a text field. This display the array elements comma-separated and placed within square brackets, "[]". Returns …

Printing array elements with toString() Read More »