[Java set] implements a simple ArrayList and a java set arraylist.

Source: Internet
Author: User

[Java set] implements a simple ArrayList and a java set arraylist.

List interface:

public interface IMyList<E> {    E set(int index, E elem);    boolean add(E e);    boolean remove(Object o);    E remove(int index);    int indexOf(Object o);    E get(int index);    int size();    boolean isEmpty();}

ArrayList implementation:

Import java. util. arrays; public class MyArrayList <E> implements IMyList <E> {private Object [] elementData; private int size; public MyArrayList () {this (10 );} public MyArrayList (int initialCapacity) {this. elementData = new Object [initialCapacity] ;}@ Override public E set (int index, E elem) {E oldElem = elementData (index); elementData [index] = elem; return oldElem;} // check whether the space is sufficient before adding the element. If not, add @ Override public boolean add (E e) {ensureCapacity (size + 1) after resizing ); elementData [size ++] = e; return true ;}@ Override public boolean remove (Object o) {if (o = null) {for (int I = 0, len = elementData. length; I <len; I ++) if (o = elementData [I]) {fastRemove (I); return true ;}} else {for (int I = 0, len = elementData. length; I <len; I ++) if (o. equals (elementData [I]) {fastRemove (I); return true ;}} return false ;}@ Override public E remove (int index) {E oldElem = elementData (index); fastRemove (index); return oldElem;} private void fastRemove (int index) {/*** the source code is like this * int numMoved = size-index-1; * if (numMoved> 0) * System. arraycopy (elementData, index + 1, elementData, index, numMoved); **/for (int I = index + 1; I <size; I ++) {elementData [I-1] = elementData [I];} elementData [-- size] = null;} @ Override public int indexOf (Object o) {for (int I = 0, len = elementData. length; I <len; I ++) if (o = elementData [I]) return I; return-1 ;}@ Override public E get (int index) {return elementData (index) ;}@ Override public int size () {return size ;}@ Override public boolean isEmpty () {return size = 0 ;} private E elementData (int index) {return (E) elementData [index];} // if there is not enough space, you need to resize it, the size after expansion is 1.5 times that of the original private void ensureCapacity (int size) {if (size> elementData. length) {int oldCapacity = elementData. length; int newCapacity = (oldCapacity> 1) + oldCapacity; elementData = Arrays. copyOf (elementData, newCapacity );}}}

Test code:

public class Main {    public static void main(String[] args) {        IMyList<String> list = new MyArrayList<String>();        for (int i=0; i<12; i++){            list.add("s" + i);        }        list.remove("s3");        for (int i=0; i<list.size(); i++){            System.out.println(list.get(i));        }    }}

Running result:

s0s1s2s4s5s6s7s8s9s10s11


View comments

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.