Introduction to JVM Languages
上QQ阅读APP看书,第一时间看更新

ArrayList usage example

Here's a simple Java language example that demonstrates some of the ArrayList methods:

    ArrayList list1 = new ArrayList();
list1.add("this is a test");
list1.add(0, "Hello");

ArrayList list2 = new ArrayList();
list2.addAll(list1);
list1.clear();

System.out.println(list1);
System.out.println(list2);
System.out.println(list2.contains("this is a test"));

Its output would be:

[]
[Hello, this is a test]
true

The first line prints [] to indicate list1 was empty, and list2 contains two Strings (in this order): Hello and this is a test. Finally, the word true will be printed to the console because this is a test is stored inside list2 of the ArrayList object.