The ArrayList of Java Collection class learning

Source: Internet
Author: User

1. Common methods and principles of ArrayList

System of package com.fish.list;import java.util.arraylist;/* collection:----------| collection  The root interface of a single-column collection----------------| list  if the collection class of the List interface is implemented, it features: orderly, repeatable. --------------------| arraylist  arraylist The bottom is to maintain an object array implementation, features: Fast query, adding and deleting slow. When to use arraylist:  if the current data is more than the query, and delete less, then use ArrayList store this batch of data. For example: University Library If the data is always a large number of increase, it is best not to use ArrayList, because the increase and deletion of ArrayList to copy a large number of elements, or create a new large-capacity array, compare the performance--------------------|  LinkedList--------------------| vector (understand)----------------| set  If you implement the set interface of the collection class,  features: unordered and cannot be duplicated. arraylist  Unique method: Ensurecapacity (int mincapacity)   This method is generally not used, if you want to specify the ArrayList default value, generally with the ArrayList (int  initialcapacity)   Construction Method TrimToSize ()    This method is also not commonly used in written questions:  when creating an object using the ArrayList parameterless constructor,  What is the default capacity of?  if the length is not enough to increase the self-growth? ArrayList the bottom layer is the maintenance of an object array implementation, when using the parameterless constructor, the default capacity of the object array is 10, when the length is not enough, the automatic growth of 0.5 times times. */  public class demo6 {    public static void  Main (String[] args)  {        arraylist list = new arraylist ();     }}


package com.fish.list;import java.util.arraylist;import java.util.iterator;/**/class book{     int id;    String name;//  name          public book (Int id, string name)  {         this.id = id;        this.name  = name;    }         @Override      public string tostring ()  {         return  "{  ISBN:" + this.id+ "  title:" + this.name+ " }";    }          @Override     public boolean equals ( Object obj)  {        book book = (book) obj;         return this.id==book.id;    }    }     //  Requirements:  Write a function to purge repeating elements in the collection. If the ISBN is the same, it is considered a repeating element. Requirement: iterators must be used when iterating through the collection elements.  get  iterator     public class demo7 {    public  static void main (String[] args)  {         Arraylist list=  new arraylist ();         list.add ( New book ("Java Programming Idea");         list.add (New book (220, " Java Core Technology "),         list.add (New book (330," deep Javaweb "));         list.add (New book, "Javas God book");         arraylist list2 = clearrepeat (list);         SYSTEM.OUT.PRINTLN ("The element of the new collection is:" + list2);     }        public static arraylist   Clearrepeat (arraylist list) {    //Create a new collection            arraylist newlist = new arraylist ();         //Gets the iterator         iterator it = list.iterator ( );         while (It.hasnext ()) {             Book book =  (book)  it.next ();   //elements obtained from the old collection                          if (!newlist.contains (book)) {                 //If the new collection does not contain the book, it is stored in the new collection           &Nbsp;      newlist.add (book);             }        }         return newlist;    }}



This article from "Small Fish Blog" blog, declined reprint!

The ArrayList of Java Collection class learning

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.