2021-06-20

1.1 List常用方法

A:添加功能

boolean add(E e):向集合中添加一個元素

void add(int index, E element):在指定位置添加元素

boolean addAll(Collection<? extends E> c):向集合中添加一個集合的元素。

B:刪除功能

void clear():刪除集合中的所有元素

E remove(int index):根據(jù)指定索引刪除元素,并把刪除的元素返回

boolean remove(Object o):從集合中刪除指定的元素

boolean removeAll(Collection<?> c):從集合中刪除一個指定的集合元素。

C:修改功能

E set(int index, E element):把指定索引位置的元素修改為指定的值,返回修改前的值。

D:獲取功能

E get(int index):獲取指定位置的元素

Iterator iterator():就是用來獲取集合中每一個元素。

E:判斷功能

boolean isEmpty():判斷集合是否為空。

boolean contains(Object o):判斷集合中是否存在指定的元素。

boolean containsAll(Collection<?> c):判斷集合中是否存在指定的一個集合中的元素。

F:長度功能

int size():獲取集合中的元素個數(shù)

G:把集合轉(zhuǎn)換成數(shù)組

Object[] toArray():把集合變成數(shù)組。

public class ArrayListTest {

? ? public static void main(String[] agrs){

? ? ? ? //創(chuàng)建ArrayList集合:

? ? ? ? List<String> list = new ArrayList<String>();

? ? ? ? System.out.println("ArrayList集合初始化容量:"+list.size());

? ? ? ? //添加功能:

? ? ? ? list.add("Hello");

? ? ? ? list.add("world");

? ? ? ? list.add(2,"!");

? ? ? ? System.out.println("ArrayList當前容量:"+list.size());

? ? ? ? //修改功能:

? ? ? ? list.set(0,"my");

? ? ? ? list.set(1,"name");

? ? ? ? System.out.println("ArrayList當前內(nèi)容:"+list.toString());

? ? ? ? //獲取功能:

? ? ? ? String element = list.get(0);

? ? ? ? System.out.println(element);

? ? ? ? //迭代器遍歷集合:(ArrayList實際的跌倒器是Itr對象)

? ? ? ? Iterator<String> iterator =? list.iterator();

? ? ? ? while(iterator.hasNext()){

? ? ? ? ? ? String next = iterator.next();

? ? ? ? ? ? System.out.println(next);

? ? ? ? }

? ? ? ? //for循環(huán)迭代集合:

? ? ? ? for(String str:list){

? ? ? ? ? ? System.out.println(str);

? ? ? ? }

? ? ? ? //判斷功能:

? ? ? ? boolean isEmpty = list.isEmpty();

? ? ? ? boolean isContain = list.contains("my");

? ? ? ? //長度功能:

? ? ? ? int size = list.size();

? ? ? ? //把集合轉(zhuǎn)換成數(shù)組:

? ? ? ? String[] strArray = list.toArray(new String[]{});

? ? ? ? //刪除功能:

? ? ? ? list.remove(0);

? ? ? ? list.remove("world");

? ? ? ? list.clear();

? ? ? ? System.out.println("ArrayList當前容量:"+list.size());

? ? }

}

1.3LinkedList基本操作

public class LinkedListTest {

? ? public static void main(String[] agrs){

? ? ? ? List<String> linkedList = new LinkedList<String>();

? ? ? ? System.out.println("LinkedList初始容量:"+linkedList.size());

? ? ? ? //添加功能:

? ? ? ? linkedList.add("my");

? ? ? ? linkedList.add("name");

? ? ? ? linkedList.add("is");

? ? ? ? linkedList.add("jiaboyan");

? ? ? ? System.out.println("LinkedList當前容量:"+ linkedList.size());

? ? ? ? //修改功能:

? ? ? ? linkedList.set(0,"hello");

? ? ? ? linkedList.set(1,"world");

? ? ? ? System.out.println("LinkedList當前內(nèi)容:"+ linkedList.toString());

? ? ? ? //獲取功能:

? ? ? ? String element = linkedList.get(0);

? ? ? ? System.out.println(element);

? ? ? ? //遍歷集合:(LinkedList實際的跌倒器是ListItr對象)

? ? ? ? Iterator<String> iterator =? linkedList.iterator();

? ? ? ? while(iterator.hasNext()){

? ? ? ? ? ? String next = iterator.next();

? ? ? ? ? ? System.out.println(next);

? ? ? ? }

? ? ? ? //for循環(huán)迭代集合:

? ? ? ? for(String str:linkedList){

? ? ? ? ? ? System.out.println(str);

? ? ? ? }

? ? ? ? //判斷功能:

? ? ? ? boolean isEmpty = linkedList.isEmpty();

? ? ? ? boolean isContains = linkedList.contains("jiaboyan");

? ? ? ? //長度功能:

? ? ? ? int size = linkedList.size();

? ? ? ? //刪除功能:

? ? ? ? linkedList.remove(0);

? ? ? ? linkedList.remove("jiaboyan");

? ? ? ? linkedList.clear();

? ? ? ? System.out.println("LinkedList當前容量:" + linkedList.size());

? ? }

}

? ? ? ? //刪除功能:

? ? ? ? linkedList.remove(0);

? ? ? ? linkedList.remove("jiaboyan");

? ? ? ? linkedList.clear();

? ? ? ? System.out.println("LinkedList當前容量:" + linkedList.size());

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 適宜閱讀人群 1、待面試的 初/中/高級 Java 程序員 2、查漏補缺的人 3、想要不斷完善和擴充自己 Java...
    安冬尼閱讀 5,139評論 1 33
  • 10.01_面向?qū)ο?package關(guān)鍵字的概述及作用)(了解) A:為什么要有包將字節(jié)碼(.class)進行分類...
    冰川_閱讀 692評論 0 1
  • 轉(zhuǎn)載自:Java集合框架實例 1- 介紹 集合是程序和語言的基本思想。應用程序通常都會應用到集合,例如雇員的信息,...
    01_小小魚_01閱讀 480評論 0 1
  • 集合框架 集合框架是為表示和操作集合而規(guī)定的一種統(tǒng)一的標準的體系結(jié)構(gòu)。任何集合框架都包含三大塊內(nèi)容:對外的接口、接...
    劉玉鵬151閱讀 311評論 0 0
  • 第十一章 常用類的概述和使用 11.1 常用包 11.1.1 包名和名稱 java.lang 包,由虛擬機自動導入...
    青山常客閱讀 315評論 0 0

友情鏈接更多精彩內(nèi)容