Reputation: 11
I've been working on an assignment which requires me to pretty much make my own ArrayList without using any built in functions (I know theres a class for it) which brings me to a problem I am having. Where the asterisks are I'm getting a "can't find symbol - method add(E)" but I'm not sure why. I've also added the two add methods also I looked at various other posts about this issue and understood little about there explanation since I'm still kind of new. Any help/pointers would be greatly appreciated!
public E[] slice(int beginIndex, int endIndex)
{
if (endIndex - beginIndex > 0)
if (beginIndex >= 0 && endIndex < size) {
E[] newList = (E[])(new Object[endIndex - beginIndex]);
for (int i = beginIndex; i < endIndex; i++)
**newList[i].add(data[i]);**
return newList;
}else
throw new IndexOutOfBoundsException();
return null;
public void add(E newValue)
{
if (size == data.length) {
E[] newData = (E[])(new Object[data.length*2]);
for (int i = 0; i < data.length; i++)
newData[i] = data[i];
data = newData;
}
data[size] = newValue;
size++;
public void add(int index, E newItem)
{
set(index, newItem);
}
Upvotes: 1
Views: 143
Reputation: 394136
newList[i]
is of type E, not of your array list type, so it doesn't have an add method.
It would make more sense to change
newList[i].add(data[i])
to
newList[i] = data[i];
Upvotes: 2