zoukankan      html  css  js  c++  java
  • How to implement an ArrayList structure in Java

    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.

     

    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.
  • 相关阅读:
    @Autowired 注解是如何实现的?
    工作 3 年的同事不懂 isEmpty 和 isBlank 的区别,我真是醉了。。
    4 个单词,谷歌返回 16 个 SQL 注入漏洞...
    MySQL 更新不成功,事务问题搞清楚了吗?
    Python Web应用如何部署?
    mapbox加载postgis矢量切片
    tile2lon:地图瓦片编号与经纬度的换算关系
    shp2pgsql:将shapefile导入postgis数据库
    postgreSQL表添加ID自增列
    postgreSQL连接配置
  • 原文地址:https://www.cnblogs.com/hephec/p/4579647.html
Copyright © 2011-2022 走看看