Saturday, June 4, 2011

Brief uses and advantages of java.util.Arrays


Hi Folks,

In this post i want to introduce you guys about one common utility class that is very helpful in sorting and searching of values in an array of any type like int,long,String etc. Simply we can state it as java.util.Arrays which is very useful in manipulating arrays such as searching,sorting and viewing all values as a list at a time without any iteration etc.

This class contains various methods to manipulate arrays of any type. For example, you have an array of string values like below

String s[] = {"hi", "ramu", "cris"};

and you want to sort these values then you don't need to write/implement any sorting algorithm instead you can simply use statement

Arrays.sort(s[]);

to sort those values. This java.util.Arrays.sort(Object[] a) method is useful to sort array of objects. Here our s[] is String type of object so we can simply use this method to sort our array.

If you want to print all the values using at a time then also you don'e need to iterate instead you can use

System.out.println(Arrays.asList(s[]);

This method java.util.Arrays.asList(T... a) is used to back the values to a List.

Like these there are many more methods by which you can easily manipulate arrays.

No comments: