Java随笔

发布时间 2023-07-19 23:16:56作者: 念文丶

2023.07.19

关于java当中size和length的使用,在工作当中,没有对size和length有一个明确的概念,总是能.出来哪一个就用哪一个。

 1 /**
 2  * .length 是数组的基本属性.
 3  * .size() 是集合的方法,集合是一个容器,用长度来形容不合适.
 4  * .length() 是字符串的方法,用于统计字符串的长度.
 5  * 只有字符串拥有.length()方法
 6  */
 7 public class Main {
 8     public static void main(String[] args) throws IOException {
 9         List<Integer> integerList = new ArrayList<>();
10         integerList.add(1);
11         integerList.add(2);
12         System.out.println("集合大小: " + integerList.size());
13         int[] score = new int[4];
14         System.out.println("数组长度: " + score.length);
15         String str = "这是一个字符串";
16         System.out.println("字符串长度: " + str.length());
17     }
18 }