Java String array: is there a size of method?

后端 未结 11 2012
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 03:38

I come from a php background and in php, there is an array_size() function which tells you how many elements in the array are used.

Is there a similar

11条回答
  •  离开以前
    2020-11-27 04:03

    All the above answers are proper. The important thing to observe is arrays have length attribute but not length method. Whenever you use strings and arrays in java the three basic models you might face are:

    1. String s=new String("vidyasagar");
      System.out.println(s.length()); // In this case we are using only String. No length attribute for Strings. we have to use length() method.
    2. int[] s=new int[10]; System.out.println(s.length); //here we use length attribute of arrays.
    3. String[] s=new String[10];
      System.out.println(s.length); // Here even though data type is String, it's not a single String. s is a reference for array of Strings. So we use length attribute of arrays to express how many strings can fit in that array.

提交回复
热议问题