简体   繁体   中英

Finding length of GenericArrayList parameter in Java

As part of a homework assignement I am adding methods to a class for GenericArrayLists.

Specifically I need to check the length of an ArrayList that is passed through my method. When I tried arrayINeedToCheck.length, it gives me an error because I have not defined that variable (arrayINeedToCheck).

I don't know where I have erred but I hope you'll be able to steer me in the right direction.

Here is my code:

    public void addAll(ArrayListGeneric<E> anotherList)
    {
    int s = (data.length - size);
    if (anotherList.length > s) {
        E[] newData = (E[])(new Object[data.length+anotherList.length]);
        for (i = 0; i < size; i++)
            newData[i] = data[i];
        data = newData;
    }

The anotherList.length is the problem. It says I haven't defined that variable. It's a parameter so I didn't think I'd need to define it again. Because its length and contents are variable, I can't just create a new, empty array list.

Help me stackoverflow, you're my only hope.

If ArrayListGeneric is anything like java.util.ArrayList , it won't have a public length field. Maybe you wanted .size() .

edit: or .size , as that one line would suggest.

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