List接口之ArrayList

 1 package testBlog;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 import java.util.List;
 6 
 7 public class Test {
 8     public static void main(String[] args) {
 9         List<String> all = new ArrayList<String>();// 子类ArrayList实例List接口
10 
11         System.out.println("长度:" + all.size());// 此时all的长度为0
12         all.add("hello");
13         all.add("welcome");
14         all.add("world");
15         System.out.println("长度:" + all.size());// 此时all的长度为3
16 
17         String str;
18         int x;
19         for (x = 0; x < all.size(); x++) {
20             str = all.get(x);
21             System.out.println(str);
22         }
23 
24     }
25 }

结果:

长度:0
长度:3
hello
welcome
world

原文地址:https://www.cnblogs.com/ssC2H4/p/8194470.html