I've been creating an Arraylist
as a public class, as that's how I've been taught. The functionality of the Arraylist
when I refer to it in another document with out variables e.g.: get()
, remove()
, size()
etc. Means I have to create it within the public class each time. Do I have to add code each time for each one, as at the moment I'm trying to create size()
, but need some help.
Or is there a way to make the Arraylist
function normally, not needing to add lines each time. The code should help explain what I mean:
import java.util.ArrayList;
public class Question1 {
private ArrayList<Question1Entry> entries;
public Question1() {
entries = new ArrayList<Question1Entry>();
}
public void add( String name, String studentNumber, String courseName, String courseID, String houseNumber, String streetName, String town, String postcode ) {
entries.add(new Question1Entry(name, studentNumber, courseName, courseID, houseNumber, streetName, town, postcode ));
}
public void remove (int index ) {
entries.remove(index);
}
public Question1Entry get(int index) {
return entries.get(index);
}
//The variable I need help creating
public Question1Entry size(int index) {
return entries.size(index);
}
public String toString( ) {
StringBuffer temp = new StringBuffer();
for (int i = 0; i < entries.size(); ++i) {
temp.append( entries.get(i).toString() + "\n" );
}
return temp.toString();
}
}
Is there a way to get around adding get()
, size()
remove()
and so on?
Thank you :)