ArrayList toArray() conversion doesn't let you use lenth()? [duplicate]

丶灬走出姿态 提交于 2019-12-25 19:36:16

问题


import java.util.*;
class Ball{
    public static void main(String args[])
    {
        ArrayList al = new ArrayList();
        al.add(new Integer(1));
        al.add(new Integer(2));
        al.add(new Integer(3));
        Object a[]= al.toArray();

        for (int i=0; i<a.length; i++)
        {
            System.out.println("Contents are  :"+ a[i]);
        }
}}

So I created an ArrayList and added a couple of elements to it. Then used toArray method and got object array a. However if I use i<a.length() instead of i<a.length, I get an error. What I don't understand is that if length() is a method, what is length?

Secondly why can't I output ArrayList elements using a for loop?

for (int i=0; i<al.length(); i++)
        {
            System.out.println("Contents are  :"+ al[i]);
        }

(over here using al.length() as well as al[i] gives an error)


回答1:


First, arrays have a length field which is final and int type. Arrays do not have a length method.

This is noted in Java Language Specification. Chapter 10. Arrays. (emphasis mine):

10.2. Array Variables

Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.

10.3. Array Creation

An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as a final instance variable length.

To access to the elements of an array, use [index]:

System.out.println(a[index]);

Second, ArrayList doesn't have a length method, but size.

To access to the elements of an ArrayList by inde, use the get method:

System.out.println(al.get(index));

Note that an ArrayList is not an array, it is a class that uses an array as container to hold the elements it will store and will add, remove, search and create a new array if it needs to hold more elements that it can.

To go through all the elements of an array or a List (interface implemented by ArrayList), it is better to use the enhanced for loop:

for (Object o : a) {
    System.out.println(o);
}
for (Object o : al) {
    System.out.println(o);
}



回答2:


Arrays have a field named length.

Lists have a method named size().

To access the third element of an array you use this: someArray[2]

To access the third element of a List, you use this: someList.get(2)

To summarize: Lists and Arrays are very different.

Please consider reading the documentation for the List interface (which ArrayList implements), where you can learn everything you need to know about Lists and their methods.




回答3:


If you're wondering why one is a method and the other a field: The size of an array is determined when it's created so a final field, i.e. one that can't be changed, is safe to be made public and accessed by users of the class. On the other hand, collections' sizes can change. Classes that implement Collection or List also have a field for size, but this field can't be final. So it has to be made private and a getter method has to be used to enforce the encapsulation. Otherwise anyone could just do something like myArrayList.size = -5; and break the implementation.



来源:https://stackoverflow.com/questions/23729508/arraylist-toarray-conversion-doesnt-let-you-use-lenth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!