I have built a static method that adds an element to an array (which has a fixed size). You don't have to look at the entire method, it just creates a new array with a greater length and adds an element at the end of it, then returns the new array.
static int[] add(int[] oldArray, int value){
int n = oldArray.length;
int[] newArray = new int[n+1];
for(int i=0; i<n; i++){
newArray[i]=oldArray[i];
}
newArray[n] = value;
return newArray;
}
The method is supposed to be used as follows
int[] a = {2, 5};
a = add(a, 7);
Now the array a has three elements instead of two, namely 2, 5 and 7. The problem is, this is still a little messy. Is there a way to implement the method as non-static (in a "predefined array class" or something? I'm not too sure how to express it better) in such a way that it would work as follows instead?
int[] a = {2, 5};
a.add(7);
I'm trying to achieve this without using ArrayLists and NestedLists.