码迷,mamicode.com
首页 > 编程语言 > 详细

自写超级数组

时间:2014-11-15 23:08:10      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   sp   java   for   strong   

  1 package com.lovo;
  2 
  3 import java.util.Arrays;
  4 
  5 /**
  6  * 超级数组
  7  * 
  8  * @author Administrator
  9  * 
 10  * @param <T>泛型参数
 11  */
 12 public class SuperArray<T> {
 13     private T[] array;
 14     private int size;
 15 
 16     /**
 17      * 超级数组的构造器
 18      */
 19     public SuperArray() {
 20         this(8);
 21     }
 22 
 23     /**
 24      * 超级数组的构造器
 25      * 
 26      * @param a指定容器的初始容量
 27      */
 28     @SuppressWarnings("unchecked")
 29     public SuperArray(int capacity) {
 30         array = (T[]) new Object[capacity];
 31     }
 32 
 33     /**
 34      * 添加元素
 35      * 
 36      * @param s指T型元素
 37      */
 38     public void add(T s) {
 39         if (size == array.length) {
 40             array = Arrays.copyOf(array, array.length != 0 ? array.length * 2
 41                     : 1);
 42         }
 43         array[size++] = s;
 44     }
 45 
 46     /**
 47      * 删除元素
 48      * 
 49      * @param s指T型元素
 50      */
 51     public void remove(T s) {
 52         for (int i = 0; i < size; ++i) {
 53             if (array[i] == s) {
 54                 for (int j = i; j < size; ++j) {
 55                     array[j] = array[j + 1];
 56                 }
 57                 --size;
 58             }
 59         }
 60     }
 61 
 62     /**
 63      * 根据下标删除元素
 64      * 
 65      * @param s指T型元素
 66      */
 67     public void removeAt(int index) {
 68         if (index < 0 || index > size) {
 69             throw new IndexOutOfBoundsException("数组下标越界" + index);
 70         }
 71         for (int i = 0; i < size; ++i) {
 72             if (i == index) {
 73                 for (int j = i; j < size; ++j) {
 74                     array[j] = array[j + 1];
 75                 }
 76                 --size;
 77             }
 78         }
 79 
 80     }
 81 
 82     /**
 83      * 清空超级数组
 84      */
 85     public void clear() {
 86         for (int i = 0; i < size; ++i) {
 87             array[i] = null;
 88         }
 89         size = 0;
 90     }
 91 
 92     /**
 93      * 判断超级数组是否为空
 94      * 
 95      * @return 超级数组为空返回true,否则false
 96      */
 97     public boolean isEmpty() {
 98 
 99         return size == 0;
100     }
101 
102     /**
103      * 查找元素
104      * 
105      * @param index查找元素坐在的下标
106      * @return T型元素
107      */
108     public T getT(int index) {
109         if (index < 0 || index > size) {
110             throw new IndexOutOfBoundsException("数组下标越界" + index);
111         }
112         return array[index];
113     }
114 
115     /**
116      * 获得超级数组的容量
117      * 
118      * @return 容量大小
119      */
120     public int getCapacity() {
121         return array.length;
122     }
123 
124     /**
125      * 获得超级数组中元素的个数
126      * 
127      * @return T型元素的个数
128      */
129     public int getSize() {
130         return this.size;
131     }
132 
133 }

测试代码

 1 package com.lovo;
 2 
 3 
 4 public class Test02 {
 5     public static void main(String[] args) {
 6         SuperArray<String> x = new SuperArray<String>(1);
 7         x.add("卡萨丁");
 8         x.add("苹果");
 9         x.add("djk");
10         x.add("宋德福");
11         x.add("水电费");
12         for (int i = 0; i < x.getSize(); i++) {
13             System.out.print(x.getT(i) + " ");
14         }
15         System.out.println();
16         
17         System.out.println(x.getSize());
18         System.out.println(x.getCapacity());
19         System.out.println();
20         
21         x.remove("Banana");
22         x.remove("收到");
23         System.out.println(x.getSize());
24         
25         x.clear();
26         System.out.println(x.getSize());
27         System.out.println(x.getCapacity());
28         
29         try {
30             x.removeAt(100);
31         } catch (Exception e) {
32             System.out.println(e.getMessage());
33         }
34 
35     }
36 }

 

自写超级数组

标签:style   blog   io   color   ar   sp   java   for   strong   

原文地址:http://www.cnblogs.com/Wqwwd/p/4100316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!