java length屬性 length()方法 size()方法


length是屬性,一般用來說明數組的長度

length()是方法,針對字符串String說的,用來求數組中某個元素的字符串長度

String str={"adfasf","sdufmd"};
System.out.println(str.length);     輸出數組長度2
System.out.prinln(str[1].length());     輸出數組中第2個字符串長度6 

size()是針對泛型集合而言,用於計算對象大小,查看泛型中有多少個元素

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
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM