ArrayList ialah pelaksanaan antara muka Senarai, yang terdapat di bawah rangka kerja pengumpulan dalam java yang membolehkan kami mengembangkan saiz tatasusunan secara dinamik, iaitu pada masa jalan. Kelas ini tersedia dalam java.util.package secara dalaman, ia menggunakan struktur data tatasusunan. ArrayList membenarkan hanya kelas pembalut dalam java dan pengguna mentakrifkan kelas.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Sintaks:
ArrayList<t> list = new ArrayList();</t>
List<t> list = new ArrayList();</t>
Kami boleh terus menggunakan contoh ArrayList atau menetapkannya kepada rujukan Senarai.
Pembina:
Dalam senarai tatasusunan, kami mempunyai tiga pembina yang tersedia, iaitu seperti berikut:
- ArrayList(int initialCapacity): Dalam ini, kita boleh menentukan panjang awal senarai tatasusunan. Tetapi kelas ArrayList menguruskannya jika saiz melebihi Kapasiti permulaan yang dinyatakan.
- ArrayList(): Dengan ini, kita boleh mencipta senarai kosong tanpa kapasiti awal, jadi dalam kes ini, initialCapacity lalai ialah 10.
- ArrayList(Collection extends E> c): Senarai koleksi.
Kaedah Java ArrayList Class
Berikut ialah kaedah kelas 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 super E> 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 extends E> 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 extends E> 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 super E> 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); } }</string>
Output:
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); } }</string></string>
Output:
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); } }</string>
Output:
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()); } }</string>
Output:
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); } } }</string>
Output:
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></e></e></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(...));
Atas ialah kandungan terperinci Kelas Java ArrayList. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Artikel ini membincangkan menggunakan Maven dan Gradle untuk Pengurusan Projek Java, membina automasi, dan resolusi pergantungan, membandingkan pendekatan dan strategi pengoptimuman mereka.

Artikel ini membincangkan membuat dan menggunakan perpustakaan Java tersuai (fail balang) dengan pengurusan versi dan pergantungan yang betul, menggunakan alat seperti Maven dan Gradle.

Artikel ini membincangkan pelaksanaan caching pelbagai peringkat di Java menggunakan kafein dan cache jambu untuk meningkatkan prestasi aplikasi. Ia meliputi persediaan, integrasi, dan faedah prestasi, bersama -sama dengan Pengurusan Dasar Konfigurasi dan Pengusiran PRA Terbaik

Artikel ini membincangkan menggunakan JPA untuk pemetaan objek-relasi dengan ciri-ciri canggih seperti caching dan pemuatan malas. Ia meliputi persediaan, pemetaan entiti, dan amalan terbaik untuk mengoptimumkan prestasi sambil menonjolkan potensi perangkap. [159 aksara]

Kelas kelas Java melibatkan pemuatan, menghubungkan, dan memulakan kelas menggunakan sistem hierarki dengan bootstrap, lanjutan, dan pemuat kelas aplikasi. Model delegasi induk memastikan kelas teras dimuatkan dahulu, yang mempengaruhi LOA kelas tersuai


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Penyesuai Pelayan SAP NetWeaver untuk Eclipse
Integrasikan Eclipse dengan pelayan aplikasi SAP NetWeaver.

Muat turun versi mac editor Atom
Editor sumber terbuka yang paling popular

ZendStudio 13.5.1 Mac
Persekitaran pembangunan bersepadu PHP yang berkuasa

VSCode Windows 64-bit Muat Turun
Editor IDE percuma dan berkuasa yang dilancarkan oleh Microsoft

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa