- public interface IList {
- /**
- * 往列表的尾部增加一个元素
- */
- void add(Object elem);
- /**
- * 获取下标所指定的元素。当下标越界时抛出java.lang.IndexOutOfBoundsException
- */
- Object get(int index);
- /**
- * 获取列表里当前的元素个数
- */
- int size();
- /**
- * 清空列表,移除列表里所有的元素
- */
- void clear();
- }
- public class MyIList implements IList {
- int l;// 存储的长度
- static IList[] list;
- static MyIList ml;
- private static String a;
- private static String b;
- private static String c;
- public MyIList() {
- list = null;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ml=new MyIList();
- ml.add(a);
- ml.add(b);
- ml.add(c);
- System.out.println( (String)ml.get(2));
- }
- @Override
- public void add(Object elem) {
- // TODO Auto-generated method stub
- l = ml.size();
- list[l] = (IList) elem;
- }
- @Override
- public Object get(int index) {
- // TODO Auto-generated method stub
- Object elem = null;
- l = ml.size();
- if (index >= l)
- throw new IndexOutOfBoundsException("输入数组下标越界");
- else {
- for (int i = 0; i < l; i++) {
- if (index == i) {
- elem = list[i];
- break;
- }
- i++;
- }
- return elem;
- }
- }
- @Override
- public int size() {
- // TODO Auto-generated method stub
- l=list.length;
- return l;
- }
- @Override
- public void clear() {
- // TODO Auto-generated method stub
- list=null;
- }
- }
复制代码请问怎么解决?
|
|