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");
}
}