How do I make a get method for an ArrayList?
I have the method header but I have no idea how to proceed.
public Object get(int i) {
}
How do I make a get method for an ArrayList?
I have the method header but I have no idea how to proceed.
public Object get(int i) {
}
What are you expecting to return from your get method? In java, ArrayList already has a get on it.. in C# also has a get defined on ArrayList but it is used differently.
I mean... if you are using an ArrayList this would be totally pointless and wrong... And i'm guessing your instructor wouldn't find it funny if you did:
public Object get(int i) {
return myArrayList.get(i);
}
or i mean you could beat around the bush and do this:
public Object get(int i) {
Object[] myArray = new Object[myArrayList.size()];
myArray = myArrayList.toArray(myArray );
return myArray[i];
}