Arraylist-vs-linkedlist

Source: Internet
Author: User

ArrayList is the implementation class of the list interface, the underlying data structure is an array, and the linkedlist underlying data structure is a two-way loop linked list.

So in the query ArrayList efficiency is high, adding and deleting LinkedList high. Because there is an index in the list, you can insert it at the specified location. When the Iterator iterator takes an element, the Iterator () in the call to ArrayList returns the Iterator interface instance, Iterator it = new Str ();

And STR is an internal private class of ArrayList, implements the iterator interface, so there is next (), Hasnext (),.. Method. And Listiterator is the iterator subinterface. In the Remove () or contain () method, the Equals comparison object in ArrayList is called for existence. Can be seen by rewriting.

public class Arraylisttest {

public static void Main (string[] args) {

List List = new MyList ();

List.add (New Person5 ("haha", 34));
List.add (New Person5 ("haha", 34));
List.add (New Person5 ("haha", 34));
System.out.println (List.remove (New Person5 ("haha", 34));
System.out.println (List.remove (New Person5 ("haha", 34));

Listiterator it =list.listiterator ();
while (It.hasnext ()) {
Person5 p = (Person5) it.next ();
System.out.println (P.getname () + "--------" +p.getage ());
}
}

}

Class Person6 {
private String name;
private int age;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public Person6 (String name, int.) {
Super ();
THIS.name = name;
This.age = age;
}

public int hashcode () {
SYSTEM.OUT.PRINTLN ("----hashcode------");
return this.age;

}

public boolean equals (Object o) {
Person5 p = (Person5) o;
SYSTEM.OUT.PRINTLN (this.name+ "------Equals---" +p.getname ());
if (! ( P instanceof Person5))
return false;
Return This.name.equals (P.getname ());
}

}

Class MyList extends arraylist{

@Override
public boolean remove (Object o) {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Call the Remove method----");
return Super.remove (o);
}


}

Arraylist-vs-linkedlist

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.