ArrayList中的toArray()转换不会让你使用lenth()? [英] ArrayList toArray() conversion doesn't let you use lenth()?

查看:104
本文介绍了ArrayList中的toArray()转换不会让你使用lenth()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

因此​​,我创建一个ArrayList,并添加了几个元素吧。然后,使用的toArray方法,得到的对象数组。但是,如果我用 I&LT;则为a.length()而不是 I&LT;则为a.length ,我得到一个错误。我不明白的是,如果长度()是一个方法,什么是长度

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?

其次,为什么不能用一个for循环I输出ArrayList的元素呢?

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

(在这里使用al.length()以及人[I]给出一个错误)

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

推荐答案

首先,阵列具有长度字段是最后 INT 键入。数组没有长度方法。

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

这是在 Java语言规范指出。 。第10章数组(重点煤矿):

10.2。数组变量

在创建一个数组对象,其长度不会改变。使数组变量指的是不同长度的阵列,以不同的数组的引用必须分配给变量

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。数组创建

数组创建前pression指定单元类型,嵌套阵列的的电平的数目和阵列的嵌套的水平中的至少一个的长度。数组的长度,可作为最后实例变量长度

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]);

二, 的ArrayList 没有一个长度的方法,但是的 尺寸

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

要访问由不知疲倦的一个的ArrayList 元素,使用的 GET 方式:

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

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

请注意,的ArrayList 不是一个数组,它是使用数组集装箱一类的持有它将存储并添加,删除,搜索和创建新阵列,如果需要持有更多的元素,它可以将元素。

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.

要经历一个数组或一个列表(接口由的ArrayList 实施)的所有元素,这是最好使用增强循环:

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

这篇关于ArrayList中的toArray()转换不会让你使用lenth()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆