<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java `ArrayList`示例 > 原文: [https://javatutorial.net/java-arraylist-example](https://javatutorial.net/java-arraylist-example) Java `ArrayList`類是實現`List`接口的可調整大小的數組。 它允許所有元素(包括`null`),并且實現所有可選列表操作。 可以在`ArrayList`上運行的大多數操作,例如`size`,`isEmpty`,`get`,`set`,`iterator`和`listIterator`都是恒定時間。 但是,`add`操作的復雜度為`O(n)`時間。與[`LinkedList`](https://javatutorial.net/java-linkedlist-example)相比,常數因子低。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ## `ArrayList`的優點 * 動態添加或刪除元素的能力。 * `ArrayList`使用數組作為其基礎實現,這使`ArrayList`類可以更快地訪問元素。 * 靈活性。 * `ArrayList`是面向對象的,因此您可以從中擴展并添加功能(如果需要)。 * 向`ArrayList`添加自定義行為是可能的,甚至很容易實現。 ## `ArrayList`的局限性 * 添加元素需要`O(n)`時間。 * `ArrayList`將數據順序存儲在內存中,因此,如果列表很大,則將需要大量連續的內存塊。 * `ArrayList`的初始容量為 10,如果不指定容量,則將受到性能限制。 每當`ArrayList`達到其自身容量時,數據將以 50% 以上的容量從舊空間復制到新空間。 ## `ArrayList`的簡單說明 ![ArrayList in Java](https://img.kancloud.cn/4e/16/4e1601cd1c7bb969572a3d1f88a90125_648x219.jpg) Java 中的`ArrayList` 從上圖可以看到,`ArrayList`中當前有 2 個元素,可以添加的最大元素數量(容量)為 8(或 6 個以上)。 ## `ArrayList`中的構造方法 1. `ArrayList()` – 構造一個初始容量為 10 的空列表。 2. `ArrayList(Collection <? extended E> c)` – 構造一個列表,該列表包含指定集合的??元素,并按集合的迭代器返回它們的順序。 3. `ArrayList(int initialCapacity)` – 構造一個具有指定初始容量的空列表。 ## `ArrayList`類中的方法 1. `boolean add(E e)`:將指定的元素附加到列表的末尾。 2. `void add(int index, E element)`:將指定的元素插入列表中的指定位置。 3. `boolean addAll(Collection <? extends E> c)`:將指定集合中的所有元素附加到列表的末尾,以指定集合的`??Iterator`返回它們的順序。 4. `boolean addAll(int index, Collection <? extends E> c)`:從指定位置開始,將集合中的所有元素插入列表。 5. `void clear()`:從列表中刪除所有元素。 6. `boolean contains(Object o)`:如果列表包含指定的元素,則返回`true`;否則返回`false`。 7. `void ensureCapacity(int minCapacity)`:如果有必要確保列表可以容納最小容量參數指定的所有元素,則增加列表的容量。 8. `void forEach(Consumer<? super E> action)`:對已循環通過的每個元素執行給定的動作。 9. `int indexOf(Object o)`:如果列表不包含元素,則返回 -1。 但是,如果元素存在于列表中,則它返回此列表中指定元素首次出現的索引。 10. `boolean isEmpty()`:如果列表不包含任何元素,則返回`true`。 11. `Iterator<E> iterator()`:以適當的順序返回列表中元素的迭代器。 12. `int lastIndexOf(Object o)`:返回列表中指定元素最后一次出現的索引;如果列表中不存在該元素,則返回 -1。 13. `boolean remove(Object o)`:從列表中刪除第一次出現的指定對象。 14. `boolean removeAll(Collection<?> c)`:從列表中刪除指定集合中包含的所有元素。 15. `boolean removeIf(Predicate<? super E> filter)`:移除此集合中所有滿足給定謂詞的元素。 16. `void removeRange(int fromIndex, int toIndex)`:刪除列表中`fromIndex`和`toIndex`之間的所有元素。(`fromIndex`–包括在內,`toIndex`–排除在外) 17. `void replaceAll(UnaryOperator<E> operator)`:使用將運算符應用于該元素的結果替換列表中的每個元素。 18. `boolean retainAll(Collection<?> c)`:僅返回指定集合中包含的列表中的元素。 19. `int size()`:返回列表的大小。 20. `void sort(Comparator<? super E> c)`:根據`Comparator`指定的順序對列表進行排序。 21. `Object[] toArray()`:返回包含列表中包含的元素的數組。 有關所有方法的文檔,請訪問 [Oracle 官方文檔頁面](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html)。 ### 使用`add()`在`ArrayList`中添加元素 ```java import java.util.* ; public class ArrayListEgTwo { public static void main ( String[] args) { // Create an ArrayList that consists of Strings ArrayList<String> animals = new ArrayList<String>(); // Capacity starts at 10, but size starts at 0 System.out.println("initial size: " + animals.size()); // Populating some of the arraylist animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("new size: " + animals.size()); } ``` **輸出**: ```java size: 0 new size: 3 ``` ### 使用`remove()`移除`ArrayList`中的元素 ```java import java.util.* ; public class RemoveExample { public static void main ( String[] args) { ArrayList<String> animals = new ArrayList<String>(); animals.add( "Elephant" ); animals.add( "Tiger" ); animals.add( "Lion" ); names.remove(1); for ( int i=0; i < animals.size(); i++ ) System.out.println( i + ": " + animals.elementAt(j) ); } } ``` **輸出**: ```java 0: Elephant 1: Lion ``` ### 使用`isEmpty()`檢查`ArrayList`是否包含元素 ```java import java.util.* ; public class isEmptyExample { public static void main ( String[] args) { ArrayList<String> animals = new ArrayList<String>(); System.out.println( "Case 0:" + animals.isEmpty()); animals.add("Tiger"); System.out.println( "Case 1:" + animals.isEmpty() ); nobby.clear(); System.out.println( "Case 2:" + animals.isEmpty() ); } } ``` **輸出**: ```java Case 0: true Case 1: false Case 2: true ``` ### 使用`indexOf(Object o)`在`ArrayList`中搜索元素 ```java import java.util.* ; public class IndexOfExample { public static void main (String[] args) { ArrayList<String> animals = new ArrayList<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("Index of 'Elephant': " + animals.indexOf("Elephant" )); System.out.println("Index of 'Lion': " + animals.indexOf("Lion")); } } ``` **輸出**: ```java Index of 'Elephant': 0 Index of 'Lion': 2 ``` ### 使用`Iterator()`遍歷`ArrayList`中的元素 ```java import java.util.* ; public class IteratorExample { public static void main ( String[] args) { ArrayList<String> animals = new ArrayList<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); // Initializing an iterator Iterator<String> iterator = animals.iterator(); // Using the iterator to visit each element while(iterator.hasNext()) System.out.println(iterator.next()); } } ``` **輸出**: ```java Elephant Tiger Lion ``` ### 使用增強的`For`循環遍歷`ArrayList`中的元素 ```java import java.util.* ; public class ForLoopExample { public static void main ( String[] args) { ArrayList<String> animals = new ArrayList<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); for (String animal : animals) System.out.println(animal); } } ``` **輸出**: ```java Elephant Tiger Lion ``` ### 使用`size()`獲取`ArrayList`的大小 ```java import java.util.* ; public class ForLoopExample { public static void main ( String[] args) { ArrayList<String> animals = new ArrayList<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("Size of ArrayList: " + animals.size()); } } ``` **輸出**: ```java Size of ArrayList: 3 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看