Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java ArrayList size() method return 0 instead of actual length of the ArrayList

I am learning Java and followed a tutorial like this:

package segovia.java.learn;

import java.util.ArrayList;

public class GroceryList {
    private ArrayList<String> groceryList = new ArrayList<String>();

    public void addGroceryItem(String item) {
        groceryList.add(item);
    }

    public void printGroceryList() {
        System.out.println("You have " + groceryList.size() + " items in your grocery list.");
        for (int i=0; i < groceryList.size(); i++) {
            System.out.println((i+1) + ". " + groceryList.get(i));
        }
    }

    public int getLength(){
        return groceryList.size();
    }
}
package segovia.java.learn;

public class Main {

    public static void main(String[] args) {
        GroceryList myGroceries = new GroceryList();
        myGroceries.addGroceryItem("apple");
        myGroceries.addGroceryItem("orange");
        myGroceries.addGroceryItem("pork");
        myGroceries.addGroceryItem("beef");
        myGroceries.printGroceryList();

        System.out.println(myGroceries.getLength());
    }
}

which worked fine. However, I thought that since the myGroceries is an ArrayList, so it should have the .size() method. So I added a line of

System.out.println(myGroceries.size());

but IntelliJ told me it’s not working. So I thought that MAYBE I should use inheritance so I changed to

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class GroceryList extends ArrayList {

now the .size() method is working, but it gave me a wrong value.

Final codes are like this:

package segovia.java.learn;

import java.util.ArrayList;

public class GroceryList extends ArrayList{
    private ArrayList<String> groceryList = new ArrayList<String>();

    public void addGroceryItem(String item) {
        groceryList.add(item);
    }

    public void printGroceryList() {
        System.out.println("You have " + groceryList.size() + " items in your grocery list.");
        for (int i=0; i < groceryList.size(); i++) {
            System.out.println((i+1) + ". " + groceryList.get(i));
        }
    }

    public int getLength(){
        return groceryList.size();
    }
}
package segovia.java.learn;

public class Main {

    public static void main(String[] args) {
        GroceryList myGroceries = new GroceryList();
        myGroceries.addGroceryItem("apple");
        myGroceries.addGroceryItem("orange");
        myGroceries.addGroceryItem("pork");
        myGroceries.addGroceryItem("beef");
        myGroceries.printGroceryList();

        System.out.println(myGroceries.getLength());
        System.out.println(myGroceries.size());
    }
}

and the getLenth() gave me 4, which is correct, while the .size() gave me 0, which is obviously wrong.
enter image description here

Can anyone help me understand why the codes work like this? I guess it’s related to the class inheritance but not sure.

>Solution :

I think there is a misunderstanding of inheritance vs composition. In this case, before you added extends ArrayList to GroceryList, your GroceryList has a list. That’s why .size() wasn’t working on it, because the GroceryList you defined doesn’t have a size method. It does have a list called groceryList though. That’s why after you addGroceryItem, the getLength method works correctly. It’s getting the size of groceryList.

After you added extends ArrayList, GroceryList now is an ArrayList, that’s why it has a size method now. But it is not the same list as groceryList. Hence getting the size of myGroceries, after addGroceryItem doesn’t give you the size you expected. Because you never added anything to the ArrayList that is myGroceries. You’ve only added thing to the groceryList that is a field in myGroceries.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading