Java Development Common Errors __java

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.