I am trying to create a Class that extends ArrayList adding two methods as defined below. However, the new BetterArrayList class does not properly read in parameters from its constructor method to create the extended object from the ArrayList<> object that was passed. What is the proper approach for passing an ArrayList<> object to a class such as this and assigning the contents of that object to the class member variables?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BetterArrayList<E> extends ArrayList<E> {
private int size;
private ArrayList<E> myArray = new ArrayList<>(size);
public BetterArrayList(ArrayList<E> passArr){
System.out.println("New BetterArrayList");
this.size = passArr.toArray().length;
this.myArray = passArr;
myArray.addAll(passArr);
}
public E pop() {
return remove(this.size() - 1);
}
public void print() {
myArray.forEach(elem ->{
System.out.println(elem);
});
}
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
// int[] ints = new int[] {1,2,3,4,5};
Integer[] ints = new Integer[] {1,2,3,4,5};
System.out.println(ints.length);
ArrayList<Integer> arrs = new ArrayList<Integer>(Arrays.asList(ints));
System.out.println(arrs.size());
BetterArrayList<Integer> BetterArray = new BetterArrayList<Integer>(arrs);
System.out.println(BetterArray.size());
BetterArray.pop();
BetterArray.print();
}
}