List的一波操做

public static void main(String[] args) {
    List<Entity> list = new ArrayList<>();
    list.add(new Entity(1));
    list.add(new Entity(2));
    list.add(new Entity(3));
    list.add(new Entity(4));
    list.add(new Entity(5));
    System.out.println("正常順序:" + list);

 

int indexOf = list.indexOf(new Entity(4));
System.out.println("new Entity(4)首次出現的下標:" + indexOf);

 

int indexOfNoExsit = list.indexOf("4");
System.out.println("集合中不存在查詢的元素下標爲:" + indexOfNoExsit);

 

boolean contains = list.contains(new Entity(4));
System.out.println("集合中是否包含new Entity(4)" + contains);

 

list.remove(1);
System.out.println("集合刪除下標爲1的元素:" + list);

 

list.remove(new Entity(4));
System.out.println("集合刪除new Entity(4)元素:" + list);

 

list.add(new Entity(6));
System.out.println("集合增長new Entity(6)元素:" + list);

 

list.add(0, new Entity(7));
System.out.println("集合下標爲0的位置增長new Entity(7)的元素:" + list);

Collections.shuffle(list);
System.out.println("打亂順序後的集合:" + list);

 

Entity entity = list.get(3);
System.out.println("查看下標爲3的元素:" + entity);

 

boolean exist = list.isEmpty();
System.out.println("集合不爲空返回值:" + exist);

 

list.clear();
System.out.println("清空list集合:" + list);

 

boolean noExist = list.isEmpty();
System.out.println("集合爲空返回值:" + noExist);

 

List numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
Object[] toArray = numbers.toArray();
System.out.println("數組的元素:"+toArray[0] + "," + toArray[1] + "," + toArray[2]);

 

相關文章
相關標籤/搜索