简体   繁体   English

在Java中查找GenericArrayList参数的长度

[英]Finding length of GenericArrayList parameter in Java

As part of a homework assignement I am adding methods to a class for GenericArrayLists. 作为作业分配的一部分,我将方法添加到GenericArrayLists的类中。

Specifically I need to check the length of an ArrayList that is passed through my method. 具体来说,我需要检查通过我的方法传递的ArrayList的长度。 When I tried arrayINeedToCheck.length, it gives me an error because I have not defined that variable (arrayINeedToCheck). 当我尝试arrayINeedToCheck.length时,它给了我一个错误,因为我没有定义该变量(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. anotherList.length是问题所在。 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. 帮助我stackoverflow,您是我唯一的希望。

If ArrayListGeneric is anything like java.util.ArrayList , it won't have a public length field. 如果ArrayListGeneric类似于java.util.ArrayList ,它将没有公共length字段。 Maybe you wanted .size() . 也许你想要.size()

edit: or .size , as that one line would suggest. 编辑:或.size ,如那一行所建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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