Arrays Tool class in Java

Source: Internet
Author: User

Arrays is an array helper class that comes with the JDK. Located under the Java.util package

This class has the following characteristics:

1. Cannot be created object

2. Most of them are static methods (like Lily, HH)

The following highlights the Aslist method

First look at the source code:

@SafeVarargs  Public static <T> list<t> aslist (T ... a) {     returnnew arraylist<> (a);}

The source code is very concise, is to create a ArrayList for you, the array as a parameter passed in.

In fact, the bottom ArrayList is an array.

Take a closer look at the construction method in ArrayList

It's weird. The arrays class itself has a private static class

The construction method

ArrayList (e[] array) {      if (array==null)          throwNew nullpointerexception ();        = Array;}

This is also very concise, directly passing over the array to its own array a

But what is the difference between this arrays$arraylist and java.util.ArrayList?

Borrow Baidu know Heptnaol Netizen's answer:

Not, the former is just an inner class for serialization, with only one argument to the constructor of the array, and no Add method, which means that it cannot dynamically extend an array of internally stored.

Okay, let's test the Aslist method.
int[] A1 =New int[]{1,2,3};//one-dimensional arraysint[] A2 =New int[]{1,2,3,4};//one-dimensional arraysint[] A3 =New int[][]{{1,2,3},{0,2,3},{2,2,3}};//two-dimensional arraysstring[] A4 =Newstring[]{"abc", "XYZ", "KPL"};//Array of stringsObject[] A5 =Newobject[]{NewString ("123"),NewString ("123"),NewString ("123")};//Object ArraySystem.out.println (arrays.aslist (A1)); System.out.println (Arrays.aslist (A2)); System.out.println (Arrays.aslist (A3)); System.out.println (Arrays.aslist (A4)); System.out.println (Arrays.aslist (A5));

The results are as follows:

[[Email protected]][[[email protected]][[[email protected], [[email protected], [[Email Protected]][abc, XYZ, KPL] [123, 123, 123]

So the question comes, why is the int array passed into aslist printing is still an array of addresses?

Because int is not a class, it cannot be matched with T (Type,java Class), so the compiler will pass int[] as a parameter, so the array cannot be resolved.

A string array and an object array are objects that are loaded, so you can be disassembled by mutable parameters. and assign to an array of a

Don't understand? OK, draw a graphic like this.

Why is the JDK developer designing this arrays, which is still puzzling about the inner class ArrayList in this area? And what does the Randomaccess interface mean? I will explain to you in the next section, thank you.

Arrays Tool class in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.