自己写的JAVA数据结构3

package edu.basejava.util;

public class ArrayList<V> implements IList<V>
{

 private int defaultSize = 10;

 private int maxSize;

 private int numInList;

 private int curr;

 private V[] listArray;

 public ArrayList()
 {
  setup(defaultSize);
 }

 public ArrayList(int size)
 {
  setup(size);
 }

 public void append(V item)
 {
  if (this.numInList > this.maxSize)
   Log.error("List is full");
  else
  {
   this.listArray[this.numInList++] = item;
   System.out.println("append the element in the "
     + (this.numInList - 1) + " position");
  }
 }

 public void clear()
 {
  this.numInList = this.curr = 0;
 }

 public int curr()
 {
  return this.curr;
 }
 public V currValue()
 {
  if (this.isInList())
  {
   return this.listArray[this.curr];

  } else
  {
   return null;
  }
 }

 public void insert(V item)
 {

  if (!this.isInList())
  {
   System.err.println("Bad value for current");
   return;
  }
  if (this.numInList > this.maxSize)
  {
   System.err.println("List is full");
   return;
  }
  for (int i = this.numInList; i > this.curr; i--)
  {
   this.listArray[i] = this.listArray[i - 1];
  }
  this.listArray[this.curr] = item;
  this.numInList++;
  System.out.println("Insert an element int the " + this.curr
    + " position");

 }

 public boolean isEmpty()
 {

  return this.numInList == 0;
 }

 public boolean isInList()
 {

  return (curr >= 0) && (curr < this.numInList);
 }

 public int length()
 {
  System.out.println("length " + this.numInList);
  return this.numInList;
 }

 public void next()
 {
  this.curr++;

 }

 public void prev()
 {
  this.curr--;

 }

 public void print()
 {

  if (this.isEmpty())
   System.out.println("()");
  else
  {
   System.out.print("( ");
   for (this.setFirst(); this.isInList(); this.next())
    System.out.print(this.currValue() + " ");
   System.out.println(")");
  }
 }

 public V remove()
 {
  if (this.isEmpty())
  {
   if (this.isInList())
   {
    V it = this.listArray[curr];
    for (int i = 0; i < this.numInList - 1; i++)
    {
     this.listArray[i] = this.listArray[i + 1];
    }
    this.numInList--;
    System.out.println("Remove the " + it + " element");
    return it;
   } else
   {
    System.err.println("No current element");
   }
  } else
  {
   System.err.println("Can not delete from empty list");
  }
  return null;
 }

 public void setFirst()
 {
  this.curr = 0;
 }

 public void setPos(int pos)
 {
  this.curr = pos;
  System.out.println("set curr position " + pos);
 }

 public void setValue(V val)
 {
  if (this.isInList())
  {
   this.listArray[this.curr] = val;
   System.out.println("set " + this.curr + " elements "
     + "with the value of " + val);
  } else
  {
   System.err.println("No current element");
  }
 }

 @SuppressWarnings("unchecked")
 private void setup(int size)
 {
  if (size < 0)
   throw new IllegalArgumentException("Illegal Capacity: " + size);
  this.maxSize = size;
  this.numInList = 0;
  this.listArray = (V[]) new Object[size];
  System.out.println("setup a list with " + size + " elements");
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值