简体   繁体   中英

Pass ArrayList<> Object to Class that extends ArrayList

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();
    }

}

You are getting confused by attempting to use both composition AND inheritance.

Put simply, composition is where you are attempting to put the ArrayList in as a class field and use it by accessing that field.

Inheritance is where you are attempting to use extends (in this case).

I modified your code to eliminate the composition and simply use extends (I changed the test print statements a bit to make it clear):

public class BetterArrayList<E> extends ArrayList<E> {

    private static final long serialVersionUID = 1L;
    
    public BetterArrayList(ArrayList<E> passArr) {
        this.addAll(passArr);
    }
    
    public E pop() {
        return this.remove(this.size() - 1);
    }
    public void print() {
        this.forEach(elem ->{
            System.out.println(elem);
        });
    }

    public static void main(String[] args) {
        // int[] ints = new int[] {1,2,3,4,5};
        Integer[] ints = new Integer[] {1,2,3,4,5};
        
        ArrayList<Integer> arrs = new ArrayList<>(Arrays.asList(ints));
        BetterArrayList<Integer> betterArr = new BetterArrayList<>(arrs);
        
        System.out.println("ArrayList: " + arrs);
        System.out.println("BetterArrayList: " + betterArr);
    
        betterArr.pop();
        System.out.println("BetterArrayList After Pop: " + betterArr);
    }

}

Output:

ArrayList: [1, 2, 3, 4, 5]
BetterArrayList: [1, 2, 3, 4, 5]
BetterArrayList After Pop: [1, 2, 3, 4]

Side Note:

I modified the name you used for your BetterArrayList instance to use proper Java naming conventions, withCasingLikeThis for a variable name, rather than CasingLikeThis .

Additionally, composition could be a perfectly valid way to do this as well, but try not to use both at the same time on the same class .

Tip to differentiate them:

Composition represents a has a relationship.

Inheritance represents a is a relationship.

Added super in constructor method to automate creation of new BetterArrayList<> object with all the attributes of the ArrayList<> object that was passed as an argument. Seems to be a simple fix, I was trying to manually inherit the attributes of the parent class instead of just invoking super.

public class BetterArrayList<E> extends ArrayList<E> {

    public BetterArrayList(ArrayList<E> passArr){
        super(passArr);
        System.out.println("New BetterArrayList");
    }
    public E pop() {
        return remove(this.size() - 1);
    }
    public void print() {
        System.out.println(this);
    }
    /**
     * 
     */
    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.print();
        BetterArray.pop();
        BetterArray.print();
    }

}

Here is My Solution. Tried to Simplify and Understandable.

ArrayMain.java

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayMain {

    public static void main(String[] args) {
        Integer[] ints = new Integer[]{1,2,3,4,5};

        ArrayList<Integer> arrs = new ArrayList<>(Arrays.asList(ints));
        BetterArrayList<Integer> betterArrayList = new BetterArrayList<>(arrs);

        System.out.println("Array List: " + arrs);

        betterArrayList.pop();
        betterArrayList.print();

        betterArrayList.pop();
        betterArrayList.print();
    }
}

Now Add BetterArrayList extending Arraylist and Add Pop and Print Statement:

import java.util.ArrayList;

public class BetterArrayList<E> extends ArrayList<E> {


    public BetterArrayList(ArrayList<E> arrs) {
        this.addAll(arrs);
    }

    public void pop() {

        System.out.println("Element Poped is: " + this.remove(this.size()-1));
    }


    public void print() {
        System.out.println("New Array List: " +  this);
    }
}
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM