In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "Java array how to turn the list", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in-depth, together to study and learn "Java array how to turn the list" bar!
Catalogue
The first way (not necessarily the best): use ArrayList.asList (strArray)
The second method (supports addition, deletion, query and modification):
The third way (through the collection tool class Collections.addAll () method (most efficient))
The fourth way is to convert the array of 3 basic types to List through the Stream stream of JDK8.
Misunderstanding of converting java array to list
Basic data types cannot be converted into lists
Second, the asList method returns a view of the array
The first way (not necessarily the best): use ArrayList.asList (strArray)
Using the Arrays tool class Arrays.asList (strArray), after the conversion is completed, you can only query and modify the List array. If you cannot add or delete, a UnsupportedOperationException exception will be thrown.
Import java.util.Arrays;import java.util.List; public static void Demo1 () {String [] str = {"fgx", "lzy"}; / / Note that this List is not the List in the Collections package, but the List interface List ints = Arrays.asList (str) in the util package; / / an error ints.add ("laopo") will be reported here;}
Add Datagram error:
Exception in thread "main" java.lang.UnsupportedOperationException
At java.util.AbstractList.add (AbstractList.java:148)
At java.util.AbstractList.add (AbstractList.java:108)
At JAVA fundamentals. New features of JDK8. Java array to List.Demo1 (Java array to List.java:20)
At JAVA fundamentals. New features of JDK8. Java array to List.main (Java array to List.java:13)
Reason for error: the return value of Arrays.asList (str) is a private static inner class java.utiil.Arrays.Arraylist in the java.util.Arrays class, which is not the java.util.ArrayList () we usually use.
Usage scenario: Arrays.asList (strArray) method can only be used after converting an array to List. There is no need to add or delete the values in it, but can only be read as a data source.
The second method (supports addition, deletion, query and modification):
Through the constructor of ArrayList, the return value of Arrays.asList (strArray) is changed from java.utilArrays.ArrayList to java.util.ArrayList.
Key code: ArrayList list = new ArrayList (Arrays.asList (strArray))
String [] str = {"fgx", "lzy"}; / / Note that this List is not the List in the Collections package, but the List interface java.util.ArrayList strings = new ArrayList (Arrays.asList (str)) in the util package; strings.add ("aop"); strings.stream (). ForEach (System.out::println)
Usage scenario: you need to add, delete, modify and query List after converting the array to List. When the amount of data in List is small, you can use it.
The third way (through the collection tool class Collections.addAll () method (most efficient))
Through Collections.addAll (arrayList, strArray) conversion, create a List of the same length based on the length of the array, and then convert the elements in the array to binary through the Collections.addAll () method, and then add them to the List, which is the most efficient method.
Public static void Demo3 () {/ / Note that the List is not the List in the Collections package, but the List interface String [] str = {"fgx", "lzy"} in the util package; java.util.ArrayList stringList = new ArrayList (str.length); Collections.addAll (stringList,str);} the fourth way is to convert the array of 3 basic types to List through JDK8's Stream stream
If the JDK version is above 1.8, use stream stream to quickly convert the following three arrays to List, namely int [], long [] and double []. Short [], byte [] and char [] are not supported in JDK1.8.
Int [] ints = {2,34,55,22,11}; long [] longs = {1,2,3}; double [] doubles = {1,2,3}; Arrays.stream (ints). Boxed (). Collect (Collectors.toList ()); Arrays.stream (longs). Boxed (). Collect (Collectors.toList ()); Arrays.stream (doubles). Boxed (). Collect (Collectors.toList ())
TIPs: why int [] can't be converted directly to List, Integer [] can be converted to List, and Integer [] can be converted to List, because generics in List must be reference types.
Java array to list misunderstanding 1. Basic data types can not be converted into lists
If you look closely, you can see that the parameter accepted by asList is a generic variable length parameter, while the basic data type cannot be generalized, as shown below:
Public class App {public static void main (String [] args) {int [] intarray = {1,2,3,4,5}; / / List list = Arrays.asList (intarray); compilation does not pass List
< int []>List = Arrays.asList (intarray); System.out.println (list);}}
Output:
[I @ 66d3c617]
This is because an array of type int is taken as a parameter, so the converted list contains only one int [] element.
Solution:
To convert an array of basic data types to an list of its wrapper type, you can use the utility method of the guava class library, as shown in the following example:
Int [] intArray = {1,2,3,4}; List list = Ints.asList (intArray); II. The asList method returns a view of the array
The view means that all operations on the list are reflected in the original array, and the list is fixed in length and does not support methods such as add, remove, and so on, to change the length.
Public class App {public static void main (String [] args) {int [] intArray = {1,2,3,4}; List list = Ints.asList (intArray); list.set (0,100); System.out.println (Arrays.toString (intArray)); list.add (5); list.remove (0);}}
Output:
[100, 2, 3, 4]
UnsupportedOperationException
UnsupportedOperationException
Reason:
Get (I)? Size ()? No problem.
Add ()?, this operation is not supported!
Why? Let's take a look at what the Arrays.asList (T... a) method does.
Well, there seems to be nothing. An instance of ArrayLIst has been returned. Take a look at a little more code:
What the heck is this? the ArrayList returned is not java.util.ArrayList, but an instance of an inner class in Arrays. I felt cheated all of a sudden.
Let's take a look at the methods of this inner class. It seems that there is no add method.
So what should I do if I want to use the add method?
Only need to use java.util.ArrayList packaging layer on the outer layer.
Finally, there is nothing wrong with the Arrays.asList method itself, it just depends on what to do with the converted list. If you just need to query the elements in list, you can use them directly; if you need to add or remove elements again, wrap a layer of java.util.ArrayList and reuse it.
Thank you for reading, the above is the content of "how to turn the list of arrays in Java". After the study of this article, I believe you have a deeper understanding of the problem of how to turn the list of arrays in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.