MartinYeung
MartinYeung

Love life Love IT IT blog: https://ithelp.ithome.com.tw/users/20119569

Java 的 length 、length()、size() 分別

三者都是取得物件的長度的方法

length :

Arrays (int[], double[], String[]) — 取得Array的長度

int[] intArray = new int[20]; 
int lengthArray = intArray.length;

length():

String related Object (String, StringBuilder, etc) — 取得字串的長度

String iAmString = "iAmString"; 
int lengthString = iAmString.length();

size():

Collection Object (ArrayList, Set, etc) — 取得集合物件相關大小

ArrayList<String> iAmArrayList = new ArrayList<String>();
    iAmArrayList.add("A1");
    iAmArrayList.add("A2");
int lengthArrayList = lengthArrayList.size();


CC BY-NC-ND 2.0

Like my work?
Don't forget to support or like, so I know you are with me..

Loading...

Comment