Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Array and Arrays class in Java? [duplicate]

Tags:

java

arrays

I'm new to Java and is learning the concept of array. I have encountered two Java classes: Array and Arrays. I was just wondering what's the difference between the two classes?

like image 618
Thor Avatar asked Mar 08 '16 06:03

Thor


2 Answers

They simply serve different purposes with a different set of methods:

java.lang.reflect.Array

The Array class provides static methods to dynamically create and access Java arrays.

This class is essentially a utility class with static methods to manipulate arrays on a lower level. It is usually used for advanced techniques where access to arrays is required through the reflection API.

java.util.Arrays

This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

This class is essentially a utility class with static methods to work on raw arrays and to provide a bridge from raw arrays to Collection based arrays (List).

For example, you can easily fill an array with a particular value:

import java.util.Arrays;
    
int[] array = new int[1024];
Arrays.fill(array, 42);

Another useful method is toString which returns a formatted representation of a given array:

System.err.println(Arrays.toString(array));  

// [42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, ...]
like image 107
Andreas Fester Avatar answered Sep 29 '22 07:09

Andreas Fester


Array :- This class can be used to create array in run time using reflection.

Arrays :- Utility class,which contains static methods to manipulate(sort,max,min etc.) the values stored in array.

like image 29
dReAmEr Avatar answered Sep 29 '22 07:09

dReAmEr