In this article, we will discuss Arrays class – a utility class for Collection framework for inter-conversion between arrays and List, which has useful methods for
- Sorting
- Searching
- Conversion to List
- etc
1. Arrays:
- All utility methods inside Arrays class are static
- Methods can be invoked directly using class-name, without creating instance
- This is mostly used for List; for inter-conversion between Arrays and List
2. Arrays method:
Arrays method | Description |
void sort(primitive[] pmt); | here, primitive can be int, short, long, double, char, byte, float Note: only natural ordering is possible for primitive types |
void sort(Object[] object); | any object implementing Comparable interface |
void sort(Object[] o, Comparator c); | to sort any object based on specified Comparator |
void binarySearch(primitive[] pmt, primitive e); | here, primitive can be int, short, long, double, char, byte, float
e –> element to be searched |
void binarySearch(Object[] oArray, object obj); | obj is the object searched from specified object array |
void binarySearch(Object[]oArray, object obj Comparator c); | obj is the object searched from specified object array based on specified Comparator |
List asList(Object[] oArray); | Used to convert array into list object but actually a List reference not separate object |
3. Advantages of Arrays :
- It is used to store one or more element/objects of same-type
- It can referred/accessed using single variable-name
- Easily any element can be accessed using index-position
- It can also be used to implement 2D matrices
4. Disadvantages of Arrays :
- Arrays size/length is fixed, which we need assign while creating/initializing
- Due to its fixed, it doesn’t increases when more elements need to be added or doesn’t get shrinks when elements are removed
- So, we all must know in advance about Arrays size, while creating/initializing itself
- Since Arrays stores elements in contagious location therefore it is time-consuming activity to insert/add or remove/delete elements
- If we under-utilised is more than its capacity, then memory gets wasted
- Similarly, it isn’t flexible to add/insert more elements
Related Articles:
- Arrays class
- Arrays v/s ArrayList
- Java – Various ways to iterate Arrays in 5 ways
- Java – How to find duplicate in String Arrays ?
- Java – Various ways to remove duplicate elements from Arrays – 5 ways
- Java – Searching element from Arrays using Binary Search Algorithm
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
Happy Coding !!
Happy Learning !!