How to resize ArrayList

Assuming the number of elements added does not exceed the specified ‘size’, there will be no need for resizing. Additionally, I would like the size of the constructor to be initialized, as it may be necessary to access it before making any modifications to the elements or adding/removing any of them.

Question:

As someone with a C++ background, my aim is to create a collection of matrices.

ArrayList> javamatrix 

In C++ I would just do

std::vector > cppmatrix;
std::vectorvcol(cols);
cppmatrix.resize(rows,vcol);

Is there an alternative collection I could use for this task since there doesn’t seem to be a built-in

resize()

function available for

ArrayLists

? Or do I have to resort to using for loops with

javamatrix.add()

to accomplish it?


I would like the size of the object to be initialized in the constructor as it may be requested before any modifications are made to its elements or before any additions or deletions occur.


Solution 1:

Automatic construction and addition of elements do not exist in

resize

. It is necessary to manually perform this task. However,

ensureCapacity

is comparable to the

reserve

of a vector, as it guarantees space availability without modifying the current size.


Solution 2:


Arraylists do not require resizing. The initial size passed in is only a starting point. Any attempts to
add items
exceeding the current size will result in automatic resizing.

From the documentation:

The capacity of an ArrayList is determined by the size of the array utilized to store the list’s elements, and it can vary depending on the list’s size. Whenever new elements are added to an ArrayList, the capacity is increased automatically. Although the exact growth policy is unspecified, it is known that the addition of an element incurs a certain cost, denoted by
Constant Amortized Time
.


Solution 3:


In general, there is usually no need to perform a ‘resize()’ operation on an ArrayList. This is because the ArrayList will automatically resize itself as you add elements to it. Additionally, it may not be clear what type of values should be stored in the ArrayList<>, as storing ‘null’ would not be particularly useful. For your specific case, you would likely need to use a loop to create MatrixCell objects regardless.

I find it puzzling that ArrayList lacks a ‘resize()’ method, as it would be helpful for readers who wish to shrink their ArrayList. However, it’s possible that this omission was intentional, so as not to confuse inexperienced programmers who might assume that ArrayList<> cannot automatically adjust its size.

In Java, this technique can help in minimizing the difference between
size of an ArrayList
.

    list.subList(n,list.size()).clear();

The reason behind its functionality is the List returned by ‘subList’ that is supported by the original ArrayList<>. Thus, the original ‘ArrayList<>’ is the one getting affected by the ‘clear()’ operation.


Solution 4:

Despite the age of the question, I suggest checking out this link
java arraylist
for assistance with the issue of ensureCapacity not working. The solution involves adding a “Null” value to adjust the current size of the code.

You have the option to use ensureSize instead of solely relying on ensureCapacity.

public static void ensureSize(ArrayList list, int size) {
    list.ensureCapacity(size);
    while (list.size() < size) {
        list.add(null);
    }
}

Frequently Asked Questions