There are a ton of different implementations of ArrayLinearLists and the likes. This is yet another. It supports a lot of the stack and resizing functions that PHP arrays support natively.
package djlosch;
/*
ObjectList is an extended version of the common arraylinearlist, but without hashing.
It supports a large amnt of functions that common ALL's have and a few other.
*/
public class ObjectList
{
//Attributes
private Object[] list;
private int size;
//Constructors
public ObjectList()
{
//build an empty objl
this.list = new Object[1];
this.size = 0;
}
public ObjectList(Object[] array)
{
//build an objl from an array of objects
this.list = new Object[array.length];
if (array.length > 0)
System.arraycopy(array, 0, this.list, 0, array.length);
this.size = array.length;
}
public ObjectList(int[] array)
{
//build an objl from an array of ints
this.list = new Object[array.length];
for(int i = 0; i < array.length; i++)
{
this.list[i] = new Integer(array[i]);
}
this.size = array.length;
}
//Functions
public void add(Object obj)
{
//add an obj to the end of an objl
if (size == 0)
{
this.size = 1;
this.list[0] = obj;
}
else
{
Object[] newList = new Object[size+1];
System.arraycopy(list, 0, newList, 0, size);
this.list = newList;
this.list[size] = obj;
this.size++;
}
}
public void remove(int index)
{
//remove the obj from an objl at specified index
if (this.size > 0)
{
Object[] theNewList = new Object[this.size];
int offset = 0;
for (int i = 0; i < this.size-1; i++)
{
if (i != index)
theNewList[i + offset] = this.list[i];
else
offset = -1;
}
this.size–;
if (this.size-1 > 0)
System.arraycopy(theNewList, 0, this.list, 0, this.size-1);
}
else
{
this.size = 0;
this.list[0] = null;
}
}
public void remove(Object obj)
{
//remove all occurances of the specified obj from an objl. function tests
//equality as opposed to virtual pointers
int i = 0;
while( i < this.size)
{
if (this.peek(i).equals(obj))
this.remove(i);
else
i++;
}
}
public void set(Object obj, int index)
{
//change the element at specified index to specified obj
if ((index >= this.size) || (index < 0))
System.out.println(”ObjectList.set => Index Invalid: ” + index);
else
this.list[index] = obj;
}
public int count(Object obj)
{
//return the number of occurances of specified obj
int count = 0;
for (int i = 0; i < this.size; i++)
{
if (list[i].equals(obj))
count++;
}
return count;
}
public Object peek(int index)
{
//return the obj at specified index. does not remove obj from objl
if ((index < this.size) && (index >= 0))
return this.list[index];
else
return null;
}
public Object peek()
{
//return the last obj in the objl. does not remove obj from objl
return peek(size-1);
}
public boolean contains(Object obj)
{
//returns whether or not an obj occurs in an objl. function tests equality
//as opposed to virtual pointers
boolean eval = false;
int i = 0;
while ((i < size) && (!eval))
{
if (this.list[i].equals(obj))
eval=true;
i++;
}
return eval;
}
public int indexOf(Object obj)
{
//returns the index of the first occurance of the specified obj. returns
//-1 if the obj is not found. function tests equality as opposed to virtual
//pointers
boolean eval = false;
int i = 0;
while ((i < size) && (!eval))
{
if (this.list[i].equals(obj))
eval=true;
i++;
}
if (eval == true)
return (i-1);
else
return -1;
}
public int[] indicesOf(Object obj)
{
//returns an int array of all indices of occurances of the specified obj.
//function tests equality as opposed to virtual pointers
int[] indices = new int[this.count(obj)];
int j = 0;
for (int i = 0; i < this.size; i++)
{
if (this.list[i].equals(obj))
{
indices[j] = i;
j++;
}
}
return indices;
}
public void insert(Object obj, int index)
{
//inserts specified obj at specified index shifting everything after
//specified index back 1 index
if ((index < 0) || (index >= size-1))
System.out.println(”ObjectList.insert: Index out of bounds: ” + index);
else
{
Object[] newList = new Object[this.size+1];
System.arraycopy(list, 0, newList, 0, index);
System.arraycopy(list, index, newList, index+1, size-index);
this.list = newList;
this.list[index] = obj;
this.size++;
}
}
public void print()
{
//prints an empty line, each index of an objl and each corresponding value,
//then another empty line
System.out.println();
for (int i = 0; i < this.size(); i++)
System.out.println(i + “: ” + this.peek(i));
System.out.println();
}
public int size()
{
//returns the size of the objl
return this.size;
}
public void size(int index)
{
//sets the size of the objl. setting a size lower than the current size
//will truncate all additional entries. setting a larger size than the
//current size will fill additional entries with a null obj.
if (index < 0)
System.out.println(”ObjectList.setSize => Index Invalid: ” + index);
else
{
String[] newList = new String[index];
if (index < this.size)
System.arraycopy(this.list, 0, newList, 0, index);
else
System.arraycopy(this.list, 0, newList, 0, this.size);
this.list = newList;
this.size = index;
}
}
public static void main(String[] args)
{
//example code use
ObjectList theList = new ObjectList();
theList.add(”stuff”);
theList.add(new Integer(5));
theList.add(”more”);
theList.add(”even more”);
theList.insert(”ahhhhhhh”, 1);
theList.insert(”a”, -1);
theList.print();
theList.size(-55);
theList.size(-585);
theList.insert(”a”, 0);
theList.add(”even more”);
theList.insert(”more”, 3);
theList.print();
System.out.println(theList.count(”even more”));
theList.remove(”more”);
theList.print();
String[] strs = new String[5];
strs[0] = “hey”;
strs[1] = “hi”;
strs[2] = “hello”;
strs[3] = “whatup”;
strs[4] = “homie g funk”;
ObjectList objStr = new ObjectList(strs);
objStr.print();
int[] ints = new int[5];
ints[0] = 10;
ints[1] = 11;
ints[2] = 12;
ints[3] = 13;
ints[4] = 14;
ObjectList objInt = new ObjectList(ints);
objInt.print();
}
}