簡體   English   中英

Java array.length 方法

[英]Java array.length method

我收到錯誤:未定義符號:長度。

import java.util.*;

class ssss

{

  public static void main(String args[])

    {

     int a[]={1,2,3,4};
     System.out.println(a[1]);
     int x = a.length();  
     System.out.println(x);
    }

}

length是數組的字段,而不是方法。 使用a.length

@TheLostMind 回答

改變

 int x = a.length(); 

int x = a.length; 

length() 不是有效的方法。 所以你可以像這樣嘗試

class ssss
{
    public static void main(String args[])
    {
        int a[]={1,2,3,4};
        System.out.println(a[1]);
        int x = a.length;  
        System.out.println(x);
    }
}

length 是公共屬性,而不是數組中的方法。 因此,我們訪問數組的長度為:

int x = a.length;

而不是,

int x = a.length();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM