zoukankan      html  css  js  c++  java
  • 自制数据结构(容器)-java开发用的最多的ArrayList和HashMap




    public class MyArrayList<E> { private int capacity = 10; private int size = 0; private E[] values = null; @SuppressWarnings("unchecked") public MyArrayList() { values = (E[]) new Object[capacity]; } @SuppressWarnings("unchecked") public MyArrayList(int capacity) { this.capacity = capacity; values = (E[]) new Object[this.capacity]; } public void put(E e) { if (e == null) { throw new RuntimeException("The value should not be null."); } if (size >= capacity) { enlargeCapacity(); } values[size] = e; size++; } public E get(int index) { if (index >= size) { throw new RuntimeException("The index:" + index + " is out of band."); } return values[index]; } public void remove(int index) { if (index >= size) { throw new RuntimeException("The index:" + index + " is out of band."); } for (int i = index; i < size - 1; i++) { values[i] = values[i + 1]; } values[size - 1] = null; size--; } @SuppressWarnings("unchecked") private void enlargeCapacity() { capacity = capacity * 2; E[] tmpValues = (E[]) new Object[capacity]; System.arraycopy(values, 0, tmpValues, 0, size); values = tmpValues; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; i < size; i++) { sb.append(values[i]).append(","); } if (size > 0) { sb.deleteCharAt(sb.length() - 1); } sb.append("]"); return sb.toString(); } /** * @param args */ public static void main(String[] args) { MyArrayList<Integer> myList = new MyArrayList<Integer>(); for(int i=0;i<1000;i++){ myList.put(i); } System.out.println(myList.toString()); } }

      

  • 相关阅读:
    SQL SERVER 2008 获取当前月,当前月前一月,当前月后一月
    SQL SERVER 2008 获取当前月,当前月前一月,当前月后一月
    SQL重复记录查询的一些方法
    TSQL日期处理总结
    50种方法巧妙优化SQL Server数据库
    DAMMIF
    CRYSOL
    Damaver
    DAMMIN
    AutoMerge
  • 原文地址:https://www.cnblogs.com/cs-lcy/p/7250181.html
Copyright © 2011-2022 走看看