java语言中
针对数组提供了length属性来获取数组的长度
针对字符串提供了length()方法来获取字符串的长度
针对泛型集合类提供了size()方法来获取元素的个数
- public class TestDemo {
- public static void testArray(int[] arr) {
- System.out.println("数组的的length属性:" + arr.length);
- }
- public static void testString(String s) {
- System.out.println("字符串中的length()方法:" + s.length());
- }
- public static void testGeneric(List list) {
- System.out.println("泛型集合中的size()方法:" + list.size());
- }
- public static void main(String[] args) throws UnsupportedEncodingException {
- int[] arr = { 1, 2, 3, 4, 5, 6 };
- String str = "12345";
- List<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(2);
- list.add(3);
- testArray(arr);// 测试数组的长度
- testString(str);// 测试字符串的长度
- testGeneric(list);// 测试list列表的长度
- }
- }
- 数组的的length属性:6
- 字符串中的length()方法:5
- 泛型集合中的size()方法:3
原文链接:点击打开链接