Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding and Using Java ArrayList: Features and Operations

Tech 2

Characteristics of Arrays

  • Random Access: Elements can be accessed in O(1) time complexity using their index without needing to traverse the entire structure.
  • Contiguous Storage: Arrays are physically and logically contiguous in memory.
  • Fixed Size: Once initialized, an array's length is immutable. Expanding an array requires allocating a new, larger array and copying the contents. In Java, arrays are objects. ArrayList acts as a resizable array container that handles this expansion automatically.
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(3);

ArrayList Overview

  1. Key Properties

    • ArrayList is a concrete implementation of the List interface.
    • It internally uses an Object[] array (default initial capacity is 10) to store elements, and it growss dynamically as needed.
    • Read operations are fast due to array indexing, but insertions and deletions in the middle are slower because they require shifting elements.
    • It is not synchronized (not thread-safe).
  2. Common Methods and Usage

    • add(E element): Appends the specified element to the end of the list.

      import java.util.ArrayList;
      
      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              System.out.println(items);
              System.out.println(items.size());
              System.out.println(items.get(1));
          }
      }
      
    • add(int index, E element): Inserts the element at the specified position, shifting subsequent elements to the right.

      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              System.out.println(items);
              items.add(1, "7");
              System.out.println(items);
          }
      }
      
    • set(int index, E element): Replaces the element at the specified position and returns the element previously at that position.

      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              System.out.println(items);
              String oldValue = items.set(1, "6");
              System.out.println(items);
              System.out.println(oldValue);
          }
      }
      
    • clear(): Removes all elements from the list.

    • isEmpty(): Returns true if the list contains no elements.

      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              System.out.println(items);
              items.clear();
              System.out.println(items.isEmpty());
              System.out.println(items);
          }
      }
      
    • contains(Object o): Returns true if the list contaisn the specified element.

      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              System.out.println(items.contains("3"));
              System.out.println(items.contains("6"));
          }
      }
      
    • remove(int index): Removes the element at the specified position, shifts subsequent elements left, and returns the removed element.

      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              items.add("56");
              System.out.println(items);
              String removed = items.remove(1);
              System.out.println(removed);
              System.out.println(items);
          }
      }
      
    • remove(Object o): Removes the first occurrence of the specified element. Returns true if the list contained the element.

      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              items.add("34");
              items.add("56");
              System.out.println(items);
              boolean isRemoved = items.remove("34");
              System.out.println(isRemoved);
              System.out.println(items);
          }
      }
      
    • iterator(): Returns an Iterator to traverse the elements in order.

      import java.util.ArrayList;
      import java.util.Iterator;
      
      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              Iterator<String> iter = items.iterator();
              while (iter.hasNext()) {
                  System.out.println(iter.next());
              }
          }
      }
      
    • Standard for loop traversal:

      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              for (int i = 0; i < items.size(); i++) {
                  System.out.println(items.get(i));
              }
          }
      }
      
    • Enhanced for loop (for-each) traversal:

      public class Demo {
          public static void main(String[] args) {
              ArrayList<String> items = new ArrayList<>();
              items.add("12");
              items.add("34");
              for (String value : items) {
                  System.out.println(value);
              }
          }
      }
      

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.