自写超级数组

  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 }

 

转载于:https://www.cnblogs.com/Wqwwd/p/4100316.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值