In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the ways of converting Java array to List". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
one。 The most common way (not necessarily the best)
Through Arrays.asList (strArray), after converting the array into List, you cannot add or delete List, you can only check and change it, otherwise an exception is thrown.
Key code: List list = Arrays.asList (strArray)
Private void testArrayCastToListError () {String [] strArray = new String [2]; List list = Arrays.asList (strArray); / / A pair of converted list inserts a data list.add ("1"); System.out.println (list);}
Execution result:
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.add (AbstractList.java:148) at java.util.AbstractList.add (AbstractList.java:108) at com.darwin.junit.Calculator.testArrayCastToList (Calculator.java:19) at com.darwin.junit.Calculator.main (Calculator.java:44)
The program throws an exception at list.add ("1"): UnsupportedOperationException.
Reason analysis:
The return value of Arrays.asList (strArray) is a private static inner class java.util.Arrays.ArrayList in the java.util.Arrays class, which is not a java.util.ArrayList class. The java.util.Arrays.ArrayList class has methods such as set (), get (), contains (), etc., but not the add add () or delete remove () methods, so calling the add () method will report an error.
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. There are 6 ways to initialize List collections in Java, which is recommended in this article. Follow the official account Java technology stack to get a series of Java collection tutorials and interview questions.
two。 After the array is converted to List, the method of adding, deleting, changing and querying is supported.
Change the return value of Arrays.asList (strArray) from java.util.Arrays.ArrayList to java.util.ArrayList through the constructor of ArrayList.
Key code: ArrayList list = new ArrayList (Arrays.asList (strArray))
Private void testArrayCastToListRight () {String [] strArray = new String [2]; ArrayList list = new ArrayList (Arrays.asList (strArray)); list.add ("1"); System.out.println (list);}
Execution result: successfully append an element "1".
[null, null, 1]
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.
three。 Through the collection utility 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.
Recommended reading: thread-safe List, no longer afraid after reading!
Key code:
ArrayList
< String>ArrayList = new ArrayList (strArray.length); Collections.addAll (arrayList, strArray)
Test:
Private void testArrayCastToListEfficient () {String [] strArray = new String [2]; ArrayList
< String>ArrayList = new ArrayList (strArray.length); Collections.addAll (arrayList, strArray); arrayList.add ("1"); System.out.println (arrayList);}
Execution result: an element "1" is also successfully appended.
[null, null, 1]
Usage scenario: after converting the array to List, you need to add, delete, modify and query List. When the amount of data in List is huge, priority is given to use, which can improve the operation speed. Follow the official account Java technology stack to get a series of Java collection tutorials and interview questions.
Note: attach the source code of Collections.addAll () method:
Public static boolean addAll (Collection
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.