java中的length属性,length()方法,size()方法

java语言中

针对数组提供了length属性来获取数组的长度

针对字符串提供了length()方法来获取字符串的长度

针对泛型集合类提供了size()方法来获取元素的个数

[java]  view plain  copy
  1. public class TestDemo {  
  2.     public static void testArray(int[] arr) {  
  3.         System.out.println("数组的的length属性:" + arr.length);  
  4.     }  
  5.   
  6.     public static void testString(String s) {  
  7.         System.out.println("字符串中的length()方法:" + s.length());  
  8.     }  
  9.   
  10.     public static void testGeneric(List list) {  
  11.         System.out.println("泛型集合中的size()方法:" + list.size());  
  12.     }  
  13.   
  14.     public static void main(String[] args) throws UnsupportedEncodingException {  
  15.         int[] arr = { 123456 };  
  16.         String str = "12345";  
  17.         List<Integer> list = new ArrayList<Integer>();  
  18.         list.add(1);  
  19.         list.add(2);  
  20.         list.add(3);  
  21.         testArray(arr);// 测试数组的长度  
  22.         testString(str);// 测试字符串的长度  
  23.         testGeneric(list);// 测试list列表的长度  
  24.     }  
  25. }  
结果:

[java]  view plain  copy
  1. 数组的的length属性:6  
  2. 字符串中的length()方法:5  
  3. 泛型集合中的size()方法:3  

原文链接:点击打开链接

猜你喜欢

转载自blog.csdn.net/qq_36688143/article/details/79798112