首頁 > Java > java教程 > 主體

Java ArrayList 類別

WBOY
發布: 2024-08-30 15:42:23
原創
316 人瀏覽過

ArrayList是List介面的實現,它屬於java中的集合框架,它允許我們動態地(即在運行時)增加陣列的大小。該類別在java.util.package內部可用,它使用陣列資料結構。 ArrayList只允許java中的包裝類,使用者定義類別。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

ArrayList<T> list = new ArrayList<>();
登入後複製
List<T> list = new ArrayList<>();
登入後複製

我們可以直接使用ArrayList的實例或將其指派給List參考。

建構子:

在陣列列表中,我們有三個可用的建構函數,如下:

  • ArrayList(int initialCapacity): 在此,我們可以指定陣列清單的初始長度。但是如果大小超過提到的initialCapacity,ArrayList 類別會處理它。
  • ArrayList(): 透過這個,我們可以建立一個沒有初始容量的空列表,所以在這種情況下,預設的initialCapacity將為10。
  • ArrayList(Collection extends E> c): 集合清單。

Java ArrayList 類別的方法

以下是java ArrayList類別的方法:

  • void trimToSize(): This method will trim the list to the current list size.
  • Object[] toArray(): Return the array of object.
  • boolean remove(Object o): This method is used to remove the object, but it will remove the first occurrence as the list contain duplicate.
  • boolean removeAll(Collection c): This method is used to remove all the elements of the list.
  • Boolean removeIf(Predicate filter): This method is used to remove the predicate passed. Used as a filter.
  • Add (E e): This method s used to add elements to the list.
  • Void add(int index, E element): It takes two-parameter and adds elements t the specific index we mentioned.
  • Boolean addAll(Collection c): This takes a list as an argument and adds all the elements to the end of the current list.
  • boolean addAll(int index, Collection c): This method adds all the elements to the current list at the specified index we pass.
  • Get (int index): This method is used to get the element. It will return the element present at the specified position in the list.
  • Int indexOf(Object o): This method is used to get the element’s index passed. It will always return the first occurrence of the element into the list because the list can contain duplicate elements.
  • ListIterator listIterator(int index): This method returns an iterator with the specific index.
  • Remove (int index): This method removes the element. It will remove the element with the corresponding index passed.
  • Protected void removeRange(int fromIndex, int toIndex): This removes the elements from a specified range.
  • Boolean retainAll(Collection c): This method will retain all elements contained in the specified collection.
  • Set (int index, E element): This method will set the element to the specified index.
  • Void sort(Comparator c): This method is used to sort the collection element.
  • List subList(int fromIndex, int toIndex): This method will be used to return the sublist from the specified index.
  • Void clear(): This mentioned is used to clear the elements in the list.
  • Object clone(): These methods create a copy of the list.
  • Boolean contains(Object o): This method is used to check whether the passing object is present in the list or not.
  • Void ensureCapacity(int minCapacity): This method is used to increase the capacity of the array list.
  • Boolean isEmpty(): This method is used to check whether the array list is empty or not.
  • Iterator iterator(): This method returns iterator.
  • int lastIndexOf(Object o): This method returns the last index of the object passed. If the object does not present in the list, it will return -1.
  • ListIterator listIterator(): This methods return iterator.

Examples of Java ArrayList Class

Examples of Java ArrayList Class are given below:

1. add an element in ArrayList

The below example will show how to add an element to an array list.

Code:

package com.cont.article;
import java.util.ArrayList;
public class ArratListTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("abc");
list.add("xyz");
list.add("yyy");
list.add("some name");
list.add("other name");
System.out.println("list is :: " + list);
}
}
登入後複製

Output:

Java ArrayList 類別

2. Copying elements of list one to another list

Code:

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("abc");
list.add("xyz");
list.add("yyy");
list.add("some name");
list.add("other name");
ArrayList<String> list2 = new ArrayList<>();
list2.addAll(list);
System.out.println("Elements in list one : " + list);
System.out.println("Elements in list two :  " + list2);
}
}
登入後複製

Output:

Java ArrayList 類別

3. Remove element from ArrayList

Code:

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("abc");
list.add("xyz");
list.add("yyy");
list.add("some name");
list.add("other name");
System.out.println("Size of list before remove ::" + list.size());
System.out.println("Elements in list are before remove " + list);
list.remove(4);
System.out.println("Size of list after removinf element :: " +list.size());
System.out.println("Elements in list are after remove" + list);
}
}
登入後複製

Output:

Java ArrayList 類別

4. Clear all the elements from ArrayList

Code:

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("abc");
list.add("xyz");
list.add("yyy");
list.add("some name");
list.add("other name");
list.clear();
System.out.println("Clering all elements of list " +list.size());
}
}
登入後複製

Output:

Java ArrayList 類別

5. Iterate all the elements of ArrayList

Code:

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("abc");
list.add("xyz");
list.add("yyy");
list.add("some name");
list.add("other name");
System.out.println("Priniting out element.");
for (String string : list) {
System.out.println("Elemnt in the list is");
System.out.println(string);
}
}
}
登入後複製

Output:

Java ArrayList 類別

The array list in java does not contain duplicate elements. Also, the insertion order is maintained in a list, which means we put over elements that will generate the output in the same sequence. Some detailed points which need to be remembered for the array list in java are as follows:

It implements various interfaces:

  • Serializable,
  • Iterable,
  • Cloneable,
  • Collection,
  • List,
  • RandomAccess

The class hierarchy is as follows:

java.lang.Object  >> java.util.AbstractCollection<E> >> java.util.AbstractList<E> >> java.util.ArrayList<E>
登入後複製

By default, the array list is not synchronized in nature and is not thread-safe, But we can make them synchronized using the collections class synchronized method. The syntax is described below for reference :

List arrList = Collections.synchronizedList (new ArrayList(...));
登入後複製

以上是Java ArrayList 類別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!