Java. Class ArrayList. Methods that determine the general characteristics of the array

Class ArrayList. Methods that determine the general characteristics of the array


Contents


Search other resources:

1. Method ensureCapacity(). Reserve a piece of memory for an array

The ensureCapacity() method sets the minimum allowable size of the ArrayList, if possible. According to the documentation, the method declaration looks like this

public void ensureCapacity(int minCapacity);

here

  • minCapacity is the desired minimum size of memory allocated for the array. The minCapacity value does not affect the current size determined by the size() method.

It is expedient to use the method in cases when the maximum possible size of the array is known in advance. This allows you to avoid unnecessary internal operations for allocating and freeing memory each time a new element is added to the array.

Example.

// ensureCapacity() method - reserve a chunk of memory for array

// 1. Create a collection of 4 lines
ArrayList<String> AL = new ArrayList();
AL.add("abc");
AL.add("def");
AL.add("jklmn");
AL.add("jprst");

// 2. Allocate the minimum array size that equals 20 elements
AL.ensureCapacity(20);

 

2. Method isEmpty(). Check if array is empty

The isEmpty() method allows you to determine if array is empty. The method declaration is as follows

public boolean isEmpty();

If the array does not contain an element, then the method returns true. Otherwise, the method returns false.

Example.

import java.util.*;

public class TrainCollections {

  public static void main(String[] args) {
    // Method isEmpty() - determine if an array is empty
    // 1. Create an empty collection
    ArrayList<Double> AL = new ArrayList();

    // 2. Determine if a collection is empty
    boolean f_empty = AL.isEmpty(); // f_empty = true
    System.out.println("f_empty = " + f_empty);

    // 3. Add an element to the collection
    AL.add(7.55);

    // 4. Re-determine if a collection is empty
    f_empty = AL.isEmpty(); // f_empty = false
    System.out.println("f_empty = " + f_empty);
  }
}

Program result

f_empty = true
f_empty = false

 

3. Method size(). Get the size of the array

The size() method returns the number of elements in an array (collection). Method declaration is as follows

public int size();

Example.

import java.util.*;

public class TrainCollections {

  public static void main(String[] args) {
    // Method size() - get the size of array
    // 1. Create array of strings
    ArrayList<String> AL = new ArrayList<String>();
    AL.add("jklm");
    AL.add("abcd");
    AL.add("elsd");
    AL.add("lkls");
    AL.add("azsd");

    // 2. Get the size of array
    int sizeArray = AL.size();
    System.out.println("sizeArray = " + sizeArray);
  }
}

Program result

sizeArray = 5

 

4. Method trimToSize(). Correct the current array size

The trimToSize() method sets the current array size to the value returned by the size() method. According to the documentation, the method declaration is as follows

public void trimToSize();

The method is used when it is necessary to optimize the size of an array after its actual size (returned by the size() method) has been significantly reduced and the array is heavily used. As a result, a smaller amount of memory will be allocated for the array instance.

Example.

import java.util.*;

public class TrainCollections {

  public static void main(String[] args) {
    // Method toArray() - get subarray based on given array
    // 1. Create an array with a capacity of up to 100 characters
    ArrayList<Character> AL = new ArrayList<Character>(100);

    // 2. Add 6 characters to array
    AL.add('0');
    AL.add('1');
    AL.add('2');
    AL.add('3');
    AL.add('4');
    AL.add('5');

    // 3. Set new array capacity
    AL.trimToSize();

    // 4. Display the array
    System.out.print("AL => ");
    for (int i=0; i<AL.size(); i++)
      System.out.print(AL.get(i) + " ");
    System.out.println();
  }
}

Program result

AL => 0 1 2 3 4 5

 


Related topics