I'm creating a program by using the generic array that allow user to define the capacity or use the default capacity. I'm running some problems with casting and methods is not exist errors when I tried to store the data into array.
Here is my constructor and methods class:
public class ArrayListADT <E>
{
public E [] element;
private int count;
private final int capacity =20;
public int cap;
public ArrayListADT()
{
count = 0;
element = (E []) new Object [capacity];
}
public ArrayListADT(int cap)
{
count = 0;
element = (E[]) new Object [cap];
}
public void resize(int k)
{
E [] ele = (E[]) new Object [k];
for(int i=0; i<element.length; i++)
{
ele [i] = element [i];
}
element = ele;
this.cap =k;
}
}
This is my main class:
public class ArrayListADTTest
{
public static void main (String [] args)
{
ArrayListADT <String> ArrayString = new ArrayListADT <String> (4);
ArrayListADT <Integer> ArrayInt = new ArrayListADT <Integer> ();
ArrayString.add("Sky");
ArrayString.resize(25);
}
}
According to my teacher, he just me to use add, but the compiler says it does not exist.
ArrayString.add("Sky");
I also trying to to do something like this:
ArrayListADT <String[]> ArrayString = new ArrayListADT <String[]> (4);
ArrayListADT <Integer[]> ArrayInt = new ArrayListADT <Integer[]> ();
ArrayString = {"clouder"};
ArrayString.resize(25);
Which give me cast errors. How can store the data into generic array? Thank you so much.