arrays.aslist ()
list<string> list = Arrays.aslist (arr);
Arrays.aslist (ARR) returns an inner class in the list-java.util.arrays.arraylist instead of-java.util.arraylist This class contains set (), Get (), contains () method, but adding and removing elements is not supported.
private static class Arraylist<e> extends Abstractlist<e> implements List<e>, Serializable, randomaccess {
...
}
However, Aslist supports overloaded java.util.Arrays.ArrayList, the correct way to open
arraylist<string> ArrayList = new Arraylist<string> (Arrays.aslist (arr));
To determine whether an array contains an element
General wording
for (String S:arr) {
if (s.equals (Targetvalue)) return
true;
}
return false;
But such code is not readable, and we think of set set containing contains methods:
Convert to set
set<string> set = new Hashset<string> (Arrays.aslist (arr));
Return Set.contains (Targetvalue);
But it's not that simple, it's a better way
Arrays.aslist (arr). Contains (Targetvalue);
That's why in the last question, arrays to write an inner class and implement some methods to remove the elements in the list error removal method
arraylist<string> list = new Arraylist<string> (Arrays.aslist ("A", "B", "C", "D"));
for (int i = 0; i < list.size (); i++) {
list.remove (i);
}
SYSTEM.OUT.PRINTLN (list);
arraylist<string> list = new Arraylist<string> (Arrays.aslist ("A", "B", "C", "D"));
for (String s:list) {
if (s.equals ("a"))
List.remove (s);
}
View ArrayList source code, found that his array structure can not be removed while traversing the correct open way to use the iterator
arraylist<string> list = new Arraylist<string> (Arrays.aslist ("A", "B", "C", "D"));
Iterator<string> iter = List.iterator ();
while (Iter.hasnext ()) {
String s = iter.next ();
if (S.equals ("a")) {
iter.remove ();
}
}
2. Reverse
for (int i=list. count-1;i>=0;i--)
{
if (List[i])
{
list.removeat (i);
}
}
3. Use Copyonwritearraylist in multi-threaded environment
public static void Main (String args[]) {
list<string> List = new copyonwritearraylist<string> ();
List.add ("A");
List.add ("B");
for (String s:list) {
if (s.equals ("B")) {
list.remove (s);
}
}
}
4.LinkedList, set?
public static void Main (String args[]) {
linkedlist<string> llist = new linkedlist<string> ();
Llist.add ("A");
Llist.add ("B");
for (String s:llist) {
if (s.equals ("B")) {
llist.remove (s);
}
}
}
Because they do not use arrays as the underlying data structure, they do not exist this problem uses the original type of the collection
1. The original type is unsafe, as follows:
public static void Add (List list, Object o) {
list.add (o);
}
public static void Main (string[] args) {
list<string> List = new arraylist<string> ();
Add (list, ten);
String s = list.get (0);
}
Java.lang.ClassCastException exception will be thrown when code runs
2.Set and set
public static void Printset (Set<?> s) {
s.add),//this line are illegal for
(Object o:s) {
System. Out.println (o);
}
However, modifying a set type does not make an error
public static void Printset (Set s) {
s.add ("2");
for (Object o:s) {
System.out.println (o);
}
}
But there's a risk access level at the start of this use
Because it is convenient to use public for all access levels, this allows for the highest access rights, but this design class is unreasonable and does not conform to the open closure principle.