Java. Class ArrayList. Methods that transform the array as a whole

Class ArrayList. Methods that transform the array as a whole


Contents


Search other resources:

1. Method clone(). Get a copy of the array

The clone() method makes a copy from an array. The copy is placed in another area of memory. According to the documentation, the general form of the method is as follows

public Object clone();

Example. The example forms an array of numbers. Then a copy of the array is made using the clone() method. In the copy, one element is changed. At the end, both arrays are output. After the result, it can be seen that the arrays have different values. This means that they are located in different areas of memory. Changing values in one array does not affect the other array.

import java.util.*;

public class TrainCollections {

  public static void main(String[] args) { 
    // Method clone() - make a copy of the collection

    // 1. Create collection of numbers
    ArrayList<Integer> AL = new ArrayList();
    AL.add(1);
    AL.add(2);
    AL.add(3);
    AL.add(4);
    AL.add(5);
    System.out.println("AL => " + AL);

    // 2. Make a copy of the collection
    ArrayList<Integer> AL2 = (ArrayList<Integer>) AL.clone();

    // 3. Print the copy AL2
    System.out.println("AL2 => " + AL2);

    // 4. Change AL2 copy
    AL2.set(2, 100); // Change another element

    // 5. Output original AL and copy AL2
    System.out.println("After change AL2: ");
    System.out.println("AL = " + AL);
    System.out.println("AL2 = " + AL2); 
  }
}

Program result

AL => [1, 2, 3, 4, 5]
AL2 => [1, 2, 3, 4, 5]
After change AL2: 
AL = [1, 2, 3, 4, 5]
AL2 = [1, 2, 100, 4, 5]

 

2. Method subList(). Get array fragment based on given array

The subList() method returns a subarray based on the given array. The general form of a method declaration is as follows

public List<E> subList(int fromIndex, int toIndex);

here

  • fromIndex, toIndex – range of values in the source array, on the basis of which the resulting array is formed.

The following features are defined for the method:

  • the value of fromIndex is considered to be included. The value of toIndex is considered excluded. For example, if you need to get the first 3 elements of an array, then fromIndex=0, toIndex=3;
  • if fromIndex and toIndex are equal, then an empty array is returned;
  • the returned list is a constituent of the original list. This means that any changes to the results will change the original list. Conversely, all changes to the original list will affect the resulting list;
  • for the resulting list, all the main operations on the list that will affect the original list are defined.

If the method is used incorrectly, the following exceptions can be thrown:

  • IndexOutOfBoundsException – the case when the value of fromIndex<0 or toIndex>size();
  • IllegalArgumentException is the case when fromIndex>toIndex.

 

Example.

import java.util.*;

public class TrainCollections {

  public static void main(String[] args) {
    // Method subList() - get a subarray based on a given array
    // 1. Create array of numbers
    ArrayList<Integer> AL = new ArrayList<Integer>();
    AL.add(10);
    AL.add(20);
    AL.add(30);
    AL.add(40);
    AL.add(50);
    AL.add(60);
    AL.add(70);

    // 2. Output the original array AL
    System.out.print("AL => ");
    for (int i=0; i<AL.size(); i++)
      System.out.print(AL.get(i) + " ");
    System.out.println();

    // 3. Get subarray
    List<Integer> AL2 = (List<Integer>) AL.subList(1, 4); // AL2 = { 20, 30, 40 }

    // 4. Print the subarray
    System.out.print("AL2 => ");
    for (int i=0; i<AL2.size(); i++)
      System.out.print(AL2.get(i) + " ");
    System.out.println();

    // 5. Change elements with indices 2, 3 in the original array,
    //    the AL2 array will also change
    AL.set(2, 55);
    AL.set(3, 88); // AL2 => 20 555 888

    // 6. Print the subarray
    System.out.print("AL2 => ");
    for (int i=0; i<AL2.size(); i++)
      System.out.print(AL2.get(i) + " ");
    System.out.println();

    // 7. Clear the AL2 array
    AL2.clear();

    // 8. Output the original 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 => 10 20 30 40 50 60 70
AL2 => 20 30 40
AL2 => 20 55 88
AL => 10 50 60 70

 

3. Method toArray(). Convert ArrayList array to Object[] type array

The toArray() method converts an ArrayList to an array of type Object[]. According to the method declaration documentation the following

public Object[] toArray();

Example.

This example creates a character array of type ArrayList. This array is then converted to an array of type Object[].

import java.util.*;

public class TrainCollections {

  public static void main(String[] args) {
    // Method toArray() - get a subarray based on a given array
    // 1. Create an array of characters
    ArrayList<Character> AL = new ArrayList<Character>();
    AL.add('0');
    AL.add('1');
    AL.add('2');
    AL.add('3');
    AL.add('4');
    AL.add('5');

    // 2. Output the original array AL
    System.out.print("AL => ");
    for (int i=0; i<AL.size(); i++)
      System.out.print(AL.get(i) + " ");
    System.out.println();

    // 3. Convert to array of type Object[]
    Object AC[] = AL.toArray();

    // 4. Output array AC
    System.out.print("AC => ");
    for (int i=0; i<AC.length; i++)
      System.out.print(AC[i] + " ");
    System.out.println();
  }
}

Program result

AL => 0 1 2 3 4 5
AC => 0 1 2 3 4 5

 

4. Method retainAll(). Form a new array containing the elements of the given collection

The retainAll() method allows you to save only the elements that are defined in the given collection in the given array. In other words, the method removes elements from the array that are not contained in the specified collection. According to the documentation, the method declaration looks like this

public boolean retainAll(Collection<?> c);

here

  • c is a collection of elements to be stored in the original array.

If the original array has changed, the method returns true.

Example.

The example performs the following actions:

  • an initial array of integers AL is created;
  • an array AL2 is created containing the numbers to be left in the array AL;
  • the retainAll() method is called for the AL array.

 

import java.util.*;
import java.util.function.*;

public class TrainCollections {

  public static void main(String[] args) {
    // Method retainAll() - form a new array based on the given
    // 1. Create a collection of integers
    ArrayList<Integer> AL = new ArrayList<Integer>();
    AL.add(2);
    AL.add(3);
    AL.add(2);
    AL.add(4);
    AL.add(7);
    AL.add(3);
    AL.add(5);
    AL.add(8);
    AL.add(4);

    // 2. Print the original collection of integers using an iterator
    Iterator<Integer> it = AL.iterator();
    System.out.print("AL => ");
    while (it.hasNext())
      System.out.print(it.next()+" ");
    System.out.println();

    // 3. Form a collection of elements to be left in the AL array
    ArrayList<Integer> AL2 = new ArrayList<Integer>();
    AL2.add(2);
    AL2.add(4);
    AL2.add(6);
    AL2.add(8);

    // 4. Output collection AL2
    System.out.print("AL2 => ");
    for (int i=0; i<AL2.size(); i++)
      System.out.print(AL2.get(i) + " ");
    System.out.println();

    // 5. Call the retainAll() method
    AL.retainAll(AL2);

    // 6. Output modified AL collection
    it = AL.iterator();
    System.out.print("AL => ");

    while (it.hasNext()) {
      System.out.print(it.next() + " ");
    }
  }
}

Program result

AL => 2 3 2 4 7 3 5 8 4
AL2 => 2 4 6 8
AL => 2 2 4 8 4

 


Related topics