Java ArrayList collection overview and basic usage basics

The difference between array and ArrayList:
 1. The length of the array cannot be changed
 2. The length of the ArrayList collection can be changed at will    

Commonly used

ArrayList()
          Construct an empty list with an initial capacity of 10.
 
 boolean add(E e)
          Add the specified element to the end of this list.

* For ArrayList, there is a pointed registered <E> for generics.
 * Generic: that is, all the elements in the collection are all of a unified type
 * Note: Generic can only be a reference type, not a basic type
 * 
 * Note;
 * For ArrayList collection, print directly What you get is not the address value, but the content.
 * If the content is empty, get the empty registration: []
 * /

public class TestArrayList {

	public static void main(String[] args) {
		//创建了一个ArrayList集合,集合名字是list ,里面装的全是String字符串类型数据
		//备注:从JDK+开始,右侧的尖挂号内部可以不写内容,但是<>本身还要写的
		ArrayList<String> list=new ArrayList<>();
		System.out.println(list);//[]      这是打印不是地址值而是 [ ]
		
//向集合当中添加一些数据,需要用到add方法
		
		list.add("甄开心");
		list.add("不高兴");
		list.add("意大利炮");
		System.out.println(list);//打印result:   [甄开心, 不高兴, 意大利炮] 
		
		//list.add(100)//是错误的写法    因为ArrayList创建的时候尖挂号泛型已经说了是字符串
	}

	}

20-4-20

Study notes

Published 10 original articles · Likes0 · Visits 88

Guess you like

Origin blog.csdn.net/Q791425037/article/details/105638802