How to implement a ArrayList structure in java-tutorial

Source: Internet
Author: User

List implementations in Java

This article describes what to implement a list data structure in Java.

The implementations in this articles is for demonstration and education purpose. They do don't try to being as efficient as the standard libraries and they is not intended to is an replacement for the Standa RD JAVA Libraries structures.

Table of Contents

1. List
2. About this website
2.1.
3. Links and Literature
1. List

The following example is contained in the project called De.vogella.datastructures.list.

A List represents a data structure which allows to dynamically add, access and remove objects of the same type. Adding objects to the list was usually done via the add() method. The get(int i) method allows to retrieve the element at position I.

Package De.vogella.datastructures.list;import Java.util.arrays;public class Mylist<e> {  private int size = 0;  private static final int default_capacity = ten;  Private Object elements[];    Public MyList () {    elements = new object[default_capacity];  }  public void Add (e e) {    if (size = = elements.length) {      Ensurecapa ();    }      elements[size++] = e;  }   private void Ensurecapa () {    int newSize = elements.length * 2;    elements = arrays.copyof (elements, newSize);  }  @SuppressWarnings ("unchecked") public  E get (int i) {    if (i>= size | | I <0) {      throw new Indexoutofboun Dsexception ("Index:" + i + ", Size" + i);    }    Return (E) elements[i];  

  

The following show contains a small JUnit test for the data structure. I Use the first Test, the MyList implementation and in the second Test, the standard Java List implementation.

 

Package De.vogella.datastructures.list;import Java.util.arraylist;import Java.util.list;import org.junit.Test;  Import static Org.junit.assert.asserttrue;public class Mylisttest {@Test (Expected=indexoutofboundsexception.class)    public void Testmylist () {mylist<integer> list = new mylist<integer> ();    List.add (1);    List.add (2);    List.add (3);    List.add (3);    List.add (4);    Asserttrue (4 = = List.get (4));    Asserttrue (2 = = List.get (1));        Asserttrue (3 = = List.get (2));  List.get (6); } @Test (Expected=indexoutofboundsexception.class) public void testnegative () {mylist<integer> list = new MyL    Ist<integer> ();    List.add (1);    List.add (2);    List.add (3);    List.add (3);    List.add (4);  List.get (-1); } @Test (Expected=indexoutofboundsexception.class) public void Testlist () {list<integer> List = new ARRAYLIST&L    T;integer> ();    List.add (1);    List.add (2);    List.add (3);    List.add (3);    List.add (4); Asserttrue (4 = = List. Get (4));    Asserttrue (2 = = List.get (1));        Asserttrue (3 = = List.get (2));  List.get (6);    }      }

  

Tip you typically also has the remove(int i)method to remove the element at position I but I leave this implementation for the reader. To-delete elements in your list just shift-elements after position I one position-the left and decrease size.

How to implement a ArrayList structure in java-tutorial

Related Article

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.