Undocumented JAVA : Arrays - A new look

Introduction

Often we have seen developers using arrays. It is a simple data structure that always we can trust and can be implemented very easily.

Java has made the handling of the array very simple. Here I am planning to bring in the features like how to optimize the usage of arrays.

Need for Arrays

We can live with the new generic collection specific to the primitive data types. Still one would argue that the java collection framework is not the only solution for handling collection. I can see a typical process where the size/type of the collection is determined in any problem then the best way to use the collection is 'Arrays'.


I am just summarizing the list of static methods in the java.util.Arrays class and with brief description of what the method does. Reference : Java doc 6



Array Manipulation

Use the methods inside the utils for better speed optimization to handle collection. Mostly these are tested and proven techniques. Like the 'fill' and 'sort' are the most useful methods which I have been using for ages and trusted.

Array to List

The '
asList' method is the best method to convert an array to java collection (java.util.List)


1
List asList(T... a) Returns a fixed-size list backed by the specified array
2 static binarySearch([] a, key) Searches the specified array of for the specified value using the binary search algorithm
3 static [] copyOf([] original, newLength) Copies the specified array, truncating or padding with default values (if necessary) so the copy has the specified length
4 static [] copyOfRange([] original, int from, int to) Copies the specified range of the specified array into a new array
5 static boolean deepEquals(Object[] a1, Object[] a2) Returns true if the two specified arrays are deeply equal to one another
6 static int deepHashCode(Object[] a) Returns a hash code based on the "deep contents" of the specified array
7 static String deepToString(Object[] a) Returns a string representation of the "deep contents" of the specified array
8 static boolean equals([] a, [] a2) Returns true if the two specified arrays of are equal to one another
9 static void fill([] a, val) Assigns the specified value to each element of the specified array of
10 static void fill([] a, int fromIndex, int toIndex, val) Assigns the specified value to each element of the specified range of the specified array of
11 static int hashCode([] a) Returns a hash code based on the contents of the specified array
12 static void sort([] a) Sorts the specified array of objects into ascending order, according to the natural ordering of its elements
13 static void sort([] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements
14 static String toString([] a) Returns a string representation of the contents of the specified array

Eg:

String[] names = {"venkat", "reflex", "demon"};
List nameList = Arrays.asList(names);


Array to String

The static toString method is good for debugging an array. If helps you from avoiding loops in our code.

Eg:

String[] names = {"venkat", "reflex", "demon"};
System.out.println("Printing Array:: " + Arrays.toString(names));

Output:

Printing Array:: [venkat, reflex, demon]

Comments

Popular posts from this blog

Java SSL/TLS Testing Tool: Cipher Suite

Developing Spring Boot Application using Docker - Part 1

Undocumented JAVA: Web Application: Deploy to the Cloud – Part 1