• 自制数据结构(容器)-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()); } }

      

  • 相关阅读:
    配置gem5-gpu模拟环境
    如何避免并发情况下的重复提交
    避免重复执行
    java线程池
    java动态代理
    Java 静态代理
    Java 静态代理和动态代理
    Spring的事务传播性
    mybatis配置(Configuration.xml)详解
    mybati之parameterType传递多个参数
  • 原文地址:https://www.cnblogs.com/cs-lcy/p/7250181.html
Copyright © 2020-2023  润新知