Write a simple ArrayList by imitating JDK -- ArrayList of Gao Qi JAVA300 lecture notes, and Gao Qi java300

Source: Internet
Author: User

Write a simple ArrayList by imitating JDK -- ArrayList of Gao Qi JAVA300 lecture notes, and Gao Qi java300

Recently, I have watched the video of Gao Qi's 300 lecture. It's time to take notes.

ArrayList is a subclass of List. It is ordered and can be repeated. The underlying implementation is an array, the thread is not secure, and the efficiency is high. Fast query, slow modification, insertion, and deletion.

The following code implements the add (), set (), isEmpty (), remove (), get (), and size () Methods of ArrayList. When Moving Elements in the array, the static method arraycopy of java. lang. System class is used to copy the array.

Put the Code directly below.

Package cn. bjsxt. collection; import java. util. date;/*** implement an ArrayList * @ author **/public class SxtArrayList/* implements List */{private Object [] elementData; // private int size of an object array; // size of ArrayList // return the size of ArrayList public int size () {return size ;} // whether ArrayList is empty public boolean isEmpty () {return size = 0;} // No parameter constructor public SxtArrayList () {this (10 ); // The default container size is 10} // The constructor loads public SxtArrayList (int initialCapacity) {if (initialCapacity <0) {try {throw new Exception ();} catch (Exception e) {e. printStackTrace () ;}} elementData = new Object [initialCapacity];} // append an Object public void add (Object obj) {ensureCapacity (); // array expansion elementData [size ++] = obj;} // insert an Object public void add (int index, Object obj) {rangeCheck (index) at the specified position ); ensureCapacity (); // resize the array // move the array from the specified position to a System. arraycopy (elementData, index, elementData, index + 1, size-index); elementData [index] = obj; size ++ ;} // obtain the public Object get (int index) {rangeCheck (index) element at the specified position; // check if the array is out of bounds return elementData [index];} // Delete the object public void remove (int index) {rangeCheck (index); // check whether the array is out of bounds int numMoved = size-index-1; // The length of the element to be moved if (numMoved> 0) {// use the array COPY method to move the element following the specified position forward to a System. arraycopy (elementData, index + 1, elementData, index, numMoved);} elementData [-- size] = null; // make the last element of the array null} // remove the overloaded public void remove (Object obj) {for (int I = 0; I <size; I ++) {if (get (I ). equals (obj) {// note: the underlying method calls the equals method instead of = remove (I) ;}// replaces the element at a certain position, return the replaced public Object set (int index, Object obj) {rangeCheck (index); Object oldValue = elementData [index]; elementData [index] = obj; return oldValue;} private void ensureCapacity () {// array resizing and data copying if (size = elementData. length) {Object [] newArray = new Object [size * 2 + 1]; // create an array with a length of 2 times and 1 System. arraycopy (elementData, 0, newArray, 0, elementData. length); // copy the elements in the old array to the new array elementData = newArray;} // check whether the array is out of bounds private void rangeCheck (int index) {if (index <0 | index> = size) {try {throw new Exception ();} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {SxtArrayList list = new SxtArrayList (3); list. add ("aaa"); list. add ("aaa"); list. add ("444"); list. add (new Date (); System. out. println (list. isEmpty (); System. out. println (list. size (); System. out. println (list. get (3); System. out. println ("replaced element:" + list. set (3, "345"); System. out. println ("the replaced element is:" + list. get (3); list. add (2, "abc"); System. out. println (list. get (2); list. remove (2); System. out. println ("####################################" ); for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I);} list. remove ("aaa"); System. out. println ("####################################" ); for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I ));}}}

 

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.