How to implement an ArrayList structure in Java - Tutorial

來源:互聯網
上載者:User

標籤:

 

List implementations in Java

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

The implementations in this articles are for demonstration and education purpose. They do not try to be as efficient as the standard libraries and they are not intended to be an replacement for the standard 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.

List represents a data structure which allows to dynamically add, access and remove objects of the same type. Adding objects to the list is 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 = 10;  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 IndexOutOfBoundsException("Index: " + i + ", Size " + i);    }    return (E) elements[i];  }} 

  

 

The following show contains a small JUnit test for the data structure. I use in 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 MyList<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<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 have 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 all elements after position i one position to the left and decrease size.

How to implement an ArrayList structure in Java - Tutorial

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.