Java array.length method

后端 未结 4 679
旧时难觅i
旧时难觅i 2021-01-29 14:05

I am getting error: Undefined symbol:length.

import java.util.*;

class ssss

{

  public static void main(String args[])

    {

     int a[]={1,2,3,4};
     Sy         


        
相关标签:
4条回答
  • 2021-01-29 14:18

    length is a public attribute not a method in array. Therefore, we access the length of array as :

    int x = a.length;
    

    and not as,

    int x = a.length();
    
    0 讨论(0)
  • 2021-01-29 14:31

    @TheLostMind answer

    change

     int x = a.length(); 
    

    to

    int x = a.length; 
    
    0 讨论(0)
  • 2021-01-29 14:31

    length() is not a valid method. So you can try like this

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-29 14:36

    length is a field for arrays and not a method. Use a.length

    0 讨论(0)
提交回复
热议问题