Discussion on the difference between Arrays. asList of and ArrayList types

  • 2020-05-12 02:39:17
  • OfStack

< pre name="code" class="html" > < pre name="code" class="html" > Arrays.asList()

Convert an array to an List object. This method returns an object of type ArrayList. The ArrayList class is not the java.util.ArrayList class, but the static inner class of the Arrays class! This object is used to add, delete, and update the list, and an UnsupportedOperationException exception is reported.


<pre name="code" class="html"> Self-test: <span>	</span>//arrayList
<span>		</span>List list = new ArrayList();
<span>		</span>list.add("yz_b_insert");
<span>		</span>list.add("yz_b_del");
<span>		</span>list.add("yz_b_update");
<span>		</span>list.add("yz_b_see");
<span>		</span>System.out.println(list.contains("yz_b_update")+"=="+list);
<span>		</span>//arrays.asList()
<span>		</span>String dd = "yz_b_insert,yz_b_del,yz_b_update,yz_b_see";
<span>		</span>List list2 = Arrays.asList(dd);
<span>		</span>System.out.println(list2.contains("yz_b_update")+"@@@@@"+list2);

Output results:


<pre name="code" class="html">arraylist : true==[yz_b_insert, yz_b_del, yz_b_update, yz_b_see]
<pre name="code" class="html">arrays.asList() : false@@@@@[yz_b_insert,yz_b_del,yz_b_update,yz_b_see]

If you use < pre name="code" class="html" > contains to determine the presence of a string in the collection, notice < pre name="code" class="html" > arraylist conversion is not available

Oneself test cannot, have tall person to be able to give directions 1

Here are the resources you can find online


<span style="color: rgb(102, 102, 102); font-family: Tahoma;"> why Arrays.asList Produced by the List It cannot be added or removed, otherwise it will be generated UnsupportedOperationException And you can explain it.  </span><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><span style="color: rgb(102, 102, 102); font-family: Tahoma;"> If we want to take 1 It gets longer or the data gets converted into List .   And expect this List Be able to add or remove Operation, how do you do that?  </span><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><span style="color: rgb(102, 102, 102); font-family: Tahoma;"> We can write 1 A similar method, which is directly adopted java.util.ArrayList Can.  </span><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><span style="color: rgb(102, 102, 102); font-family: Tahoma;"> Such as:  </span><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><pre class="java" name="code" style="color: rgb(102, 102, 102); background-color: rgb(255, 255, 255);">import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyArrays {
	public static <T> List<T> asList(T... a) {
		List<T> list = new ArrayList<T>();
		Collections.addAll(list, a);
		return list;
	}
}

The test code is as follows:


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
		print(stooges);
		List<List<String>> seasonsList = Arrays.asList(retrieveSeasonsList());
		print(seasonsList);
		/*
		 *  Their implementation 1 a asList Method that can be added and removed. 
		 */
		List<String> list = MyArrays.asList("Larry", "Moe", "Curly");
		list.add("Hello");
		print(list);
	}
	private static <T> void print(List<T> list) {
		System.out.println(list);
	}
	private static List<String> retrieveSeasonsList() {
		List<String> seasonsList = new ArrayList<String>();
		seasonsList.add("Spring");
		seasonsList.add("Summer");
		seasonsList.add("Autumn");
		seasonsList.add("Winter");
		return seasonsList;
	}
}

Output results:

[Larry, Moe, Curly]
[[Spring, Summer, Autumn, Winter]]
[Larry, Moe, Curly, Hello]


Related articles: