1. 程式人生 > >面試題:陣列有沒有length()方法? 字串有沒有length()方法? 集合有沒有length()方法?

面試題:陣列有沒有length()方法? 字串有沒有length()方法? 集合有沒有length()方法?

陣列求長度用length屬性

字串求長度用length()方法

集合求長度用size()方法

程式舉例:

package 集合.length_size;

import java.util.ArrayList;
import java.util.List;

public class Length_Size {
	public static void main(String[] args) {
		String[] strings = {"aaa","bbb","ccc"};
		String string = "aaabbbccc";
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		
		System.out.println("String[].length="+strings.length);
		System.out.println("String.length()="+string.length());
		System.out.println("List.size()="+list.size());
		
	}

}