Copyright Notice:

This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.

Every week will be uniformly updated here, if you like, you can follow the public account to get the latest article.

Shall not be reproduced without permission.

One, foreword

The Array-to-list class provides sorting and searching of Arrays. There is also a handy Api for converting an Array to a List from array.asList (), but there are a few caveat s to using it.

This article takes a look at some considerations for using Arrays.

What should we pay attention to

1. AsList () cannot convert a primitive type to List

AsList () converts an array to the List we need. But there is a problem if the array is a primitive data type.

Here’s an example:

/arr-demo0.png

The final output is:

I/cxmyDev: list size : 1Copy the code

This result is not in line with our expectations, let’s take a look at the implementation of asList() source code.

/arr-aslist.png

As you can see, asList() accepts a generic variable length argument, whereas primitive data types cannot be genericized. For generics, the basic data type is actually assumed to be of type [x. [means this is an array, and x is the type of the current array. For example, int[] is actually of type [I.

Let me give you an example.

/arr-demo1.png

I/cxmyDev: list size : 1
I/cxmyDev: list.get(0) class : [I
I/cxmyDev: list type :trueCopy the code

As you can see, this is indeed consistent with what was introduced earlier.

Therefore, it is concluded that the eight basic data types cannot be used in the method’s generic variable-length argument, or they would be assumed to be an [X type argument rather than a variable-length argument.

There are times when you need to operate on these primitive data types, when you can use wrapper classes for the primitive data types.

A simple rewrite of the above Demo.

/arr-demo2.png

Let’s look at the output and verify it.

list size : 6
I/cxmyDev: list.get(0) class : java.lang.Integer
I/cxmyDev: list type :falseCopy the code

This way, it’s exactly what we expect.

2. The list returned by asList() is immutable

Looking at the signature of asList(), you can see that it returns an ArrayList. It’s important to note, however, that this ArrayList is not the ArrayList we use all the time. The ArrayList we use all the time is under the java.util package, and asList() just returns an inner class of Arrays.

Array.arraylist () is an immutable List even though they are both arrayLists. AbstractList is also derived from AbstractList, but it only implements part of the methods and does not implement the add(), remove() and other methods used to manipulate ArrayList.

/arr-demo3.png

The above code, for example, want to asList () returns a List, and then insert a data, will throw an UnsupportedOperationException this exception.

/arr-crash.png

UnsupportedOperationException this exception, AbstractList. The add () method from them.

/arr-excep.png

As you can see, if AbstractList is inherited without implementing its corresponding method, the call will throw this exception.

If you need to use the asList() method, you need to ensure that the converted List will not be modified, otherwise an exception will be thrown.

If you want to do something with the ArrayList of arrays.asList () conversion, you’ll need to convert it again.

/arr-demo4.png

Third, summary

The pits used by arrays.aslist () are pretty much clear and provide a solution.

Copyright Notice:

This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.

Every week will be uniformly updated here, if you like, you can follow the public account to get the latest article.

Shall not be reproduced without permission.

Qr code. JPG