Home  >  Article  >  Java  >  Does Java array have length method?

Does Java array have length method?

(*-*)浩
(*-*)浩Original
2019-11-12 11:30:334953browse

Does Java array have length method?

#There is no length() method for arrays in Java, only the length attribute. Array array.length returns the length of the array.

String has a length() method, and str.length() returns the length of the string. (Recommended learning: java course)

Does Java array have length method?

size() is for generic collections and is used to calculate the object size. Check the generics How many elements.

public static void main(String[] args) {
    String []list={"ma","cao","yuan"};
    String a="macaoyuan";
    System.out.println(list.length);      数组长度是3
    System.out.println(a.length());       字符串长度是9
    List<Object> array=new ArrayList();
    array.add(a);
    System.out.println(array.size());       泛型中对象个数是1
}

The above is the detailed content of Does Java array have length method?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn