Chinaunix首页 | 论坛 | 博客
  • 博客访问: 741724
  • 博文数量: 130
  • 博客积分: 2951
  • 博客等级: 少校
  • 技术积分: 1875
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 18:32
文章分类

全部博文(130)

文章存档

2013年(1)

2012年(129)

分类: Java

2012-02-29 09:44:58

  1. public class Lhist<V> {
  2.     private V[] array;
  3.     private int size;

  4.     public Lhist(int capacity) {
  5.         array = (V[]) new Object[capacity];
  6.     }

  7.     public void add(V value) {
  8.         if (size == array.length)
  9.             throw new IndexOutOfBound***ception(Integer.toString(size));
  10.         else if (value == null)
  11.             throw new NullPointerException();
  12.         array[size++] = value;
  13.     }

  14.     public void remove(V value) {
  15.         int removalCount = 0;
  16.         for (int i=0; i<size; i++) {
  17.             if (array[i].equals(value))
  18.                 ++removalCount;
  19.             else if (removalCount > 0) {
  20.                 array[i-removalCount] = array[i];
  21.                 array[i] = null;
  22.             }
  23.         }
  24.         size -= removalCount;
  25.     }

  26.     public int size() { return size; }

  27.     public V get(int i) {
  28.         if (i >= size)
  29.             throw new IndexOutOfBound***ception(Integer.toString(i));
  30.         return array[i];
  31.     }
  32. }

使用:

Lhist li = new Lhist(30);
阅读(1462) | 评论(0) | 转发(0) |
0

上一篇:JAVA 泛型

下一篇:JAVA 泛型 - 纠错

给主人留下些什么吧!~~