I am creating an array list without actually importing \"ArrayList\" and I am st
ID: 3627690 • Letter: I
Question
I am creating an array list without actually importing "ArrayList" and I am stuck...I have a class called customer, item, and array list. each customer can only have 3 items at a time. One place I am stuck is I have a generic array called E [] array.In trying to create an add method how do I use E[] array in a generic method so that I can add items to it? It doesn't seem to want to act like the basic arrays I am use to. I keep getting:
"non-static variable list cannot be referenced from a static "
I am just attempting to add an item to the current position of the array.
array[currentPosition] = item; The method itself is a generic method. Any suggestions???
Explanation / Answer
can you post your code?
If it is array list style all you have to do is keep a pointer
Use a getArray function to access your data
public class item
{
private object []; // object is just generic sub in whatever
// you need
private int pointer ;
public item()
{array [] = new array[3]
array[0] = -1;
array[1] = -1;
array[2] = -1;
pointer = 0;
}
public void add(object e)
{
array[pointer] = object;
pointer++;
if( pointer > 3)
{
pointer = 0;
}
}
public Object[] getArray(){
//array = item.getArray() assigned to a new array would make a copy in your main
return array;
}
public object getData(int c){//returns data at index of c
if(c<3)
{
System.out.println("Array out of bounds")
return -1; //-1 means return an error
}
return array[c];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.