MyArrayList(Day13)

package Day12;

import java.util.*;

public class MyArrayList implements List {
 private Object[] data;
 private int count;

 public MyArrayList() {
  this(10);
 }

 public MyArrayList(int initCal) {
  data = new Object[initCal];
  count = 0;
 }

 public MyArrayList(Collection c) {
  data = new Object[c.size()];
  for (Object obj : c) {
   data[count++] = obj;
  }
 }

 public void add(int arg0, Object arg1) {
  if (arg0 > count || arg0 < 0) {
   throw new ArrayIndexOutOfBoundsException("参数不合法");
  }// System.out.println(3333);
  if (count == data.length) {
   Object[] obj = new Object[data.length * 2];
   //
   for (int i = 0; i < data.length; i++) {// //
    obj[i] = data[i];// ///System.arraycopy(data,0,temp,0,data.length);
   }// /
   data = obj;
  }
  for (int i = count; i > arg0; i--) {
   data[i] = data[i - 1];
  }
  data[arg0] = arg1;
  count++;
 }

 public boolean add(Object arg0) {
  // if(count==data.length){
  // Object[] obj=new Object[data.length*2];
  // // obj=data.clone();
  // for(int i=0;i<data.length;i++){
  // obj[i]=data[i];
  // }
  // data=obj;
  // }
  // data[count++]=arg0;

  add(count, arg0);
  return true;
 }

 public boolean addAll(Collection arg0) {
  // TODO Auto-generated method stub
  return false;
 }

 public boolean addAll(int arg0, Collection arg1) {
  // TODO Auto-generated method stub
  return false;
 }

 public void clear() {
  data = new Object[10];
  count = 0;
  System.gc();
 }

 public boolean contains(Object arg0) {

  // for (Object obj : data) {
  // ???System.out.println(obj);
  for (int i = 0; i < count; i++) {
   if (data[i].equals(arg0)) {
    return true;
   }
  }
  return false;
 }

 public boolean containsAll(Collection arg0) {
  // TODO Auto-generated method stub
  return false;
 }

 public Object get(int arg0) {
  if (arg0 >= count || arg0 < 0) {
   throw new ArrayIndexOutOfBoundsException("参数不合法");
  }
  return data[arg0];
 }

 public int indexOf(Object arg0) {
  for (int i = 0; i < count; i++) {
   if (data[i].equals(arg0)) {
    return i;
   }
  }
  throw new ArrayIndexOutOfBoundsException("参数不合法");
 }

 public boolean isEmpty() {
  return count == 0;
 }

 public Iterator iterator() {
  
  return new Iterator() {
   int i=0;
   public boolean hasNext() {
    return i<count;
   }

   public Object next() {
    return data[i++];
   }

   public void remove() {//把当前找到的元素删掉
    MyArrayList.this.remove(--i);//在内部类中调用外部类的属性或方法
   }
  };
 }

 public int lastIndexOf(Object arg0) {
  // TODO Auto-generated method stub
  return 0;
 }

 public ListIterator listIterator() {
  // TODO Auto-generated method stub
  return null;
 }

 public ListIterator listIterator(int arg0) {
  // TODO Auto-generated method stub
  return null;
 }

 public Object remove(int arg0) {
  if (arg0 >= count || arg0 < 0) {
   throw new ArrayIndexOutOfBoundsException("参数不合法");
  }
  Object temp = data[arg0];
  for (int i = arg0; i < count - 1; i++) {
   data[i] = data[i + 1];
  }
  count--;
  return temp;
 }

 public boolean remove(Object arg0) {
  try {
   int i = this.indexOf(arg0);
   remove(i);
   return true;
  } catch (ArrayIndexOutOfBoundsException e) {
   return false;
  }
 }

 public boolean removeAll(Collection arg0) {
  // TODO Auto-generated method stub
  return false;
 }

 public boolean retainAll(Collection arg0) {
  // TODO Auto-generated method stub
  return false;
 }

 public Object set(int arg0, Object arg1) {
  if (arg0 > count || arg0 < 0) {
   throw new ArrayIndexOutOfBoundsException("参数不合法");
  }
  Object temp = data[arg0];
  data[arg0] = arg1;
  return temp;
 }

 public int size() {
  return count;
 }

 public List subList(int arg0, int arg1) {
  // TODO Auto-generated method stub
  return null;
 }

 public Object[] toArray() {
  Object[] obj = new Object[count];
  for (int i = 0; i < count; i++) {
   obj[i] = data[i];
  }
  return obj;
 }

 public Object[] toArray(Object[] arg0) {
  // TODO Auto-generated method stub
  return null;
 }

}

package Day12;

public class MyArrayListTest {
 public static void main(String[] args) {
  MyArrayList mal = new MyArrayList(3);
  try {
   for (int i = 0; i < 5; i++) {
    mal.add(i * 3);
   }
   mal.add(3, 100);
  } catch (Exception e) {
   System.out.println("I catch1:"+e);
  }

  try
  {
  for(int i=0;i<mal.size();i++){
   System.out.println(mal.get(i));
  }
  }catch(Exception e){
   System.out.println("I catch2:"+e);
  }
  
  System.out.println(mal.contains(100));
  System.out.println(mal.size());
  for(Object o:mal.toArray()){
   System.out.print(o+"/t");
  }
  System.out.println("//");
  mal.set(3, 200);
  for(Object o:mal.toArray()){
   System.out.print(o+"/t");
  }
  System.out.println("//");
  mal.remove(3);
  for(Object o:mal.toArray()){
   System.out.print(o+"/t");
  }
  System.out.println("//");
  Integer i=9;
  mal.remove(3);
  for(Object o:mal.toArray()){
   System.out.print(o+"/t");
  }
  System.out.println("//");
  System.out.println(mal.isEmpty());
  
  System.out.println("//");
  System.out.println(mal.get(3));
  System.out.println("===========================");
  for(Object obj:mal){
   System.out.println("/t"+obj);
  }
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值