James Liang - ICS Blog

ICS4U Blogging

Arrays - INTU

Question

How could I resize arrays to fit different scenarios?

I’ve learned that once you initialize an array it has a set specific size. The array will work just fine as long as you stay within its limits; however, this could pose issues when more data needs to be added to an existing array. It seems impractical to set all arrays to insane sizes just in case you need more space in the future. That is why I wanted to see if I could resize an array to fit future scenarios and needs.

Explanation

After searching a bit online, it seemed that there were quite a few ways to achieve what I wanted to accomplish.

Method 1:

This is what I initially thought of as a solution to resizing arrays. It involves creating a new array and copying over the contents of the original array to it.

Example Code

class Main {
  public static void main(String[] args) {
    int[] testArray = {42, 27, 31};
    int newArray [] = new int [testArray.length+1];
    for (int temp = 0; temp < testArray.length; temp++)
    {
      newArray[temp]=testArray[temp];
    }
    newArray[newArray.length-1]=67;
    testArray = newArray;
    for (int temptwo = 0; temptwo < testArray.length; temptwo++)
    {
      System.out.println(testArray[temptwo]);
    }
  }
}

I first created an integer array with three values. I wanted to add a number to the end of the array, so I created a new integer array with one more space compared to the original one. I then used a for loop that went through all the values of the original array and copied it to the new array. After that line, I added the new value to the end of the array (-1 since indexes start at 0). I was then able to set the old array to the new array. Finally, I used a for loop to go through the entire array to ensure that everything worked.

This method of adding to an array works, but I think it could be replaced with something better. It requires a lot code and looks quite bulky.

Visual

Method 2:

https://www.educative.io/edpresso/how-to-append-to-an-array-in-java

This method uses the Arrays.copyOf method from the importing the Java Util Arrays.

Example Code

import java.util.Arrays;

class Main {
  public static void main( String args[] ) {
    int[] testArray = {42, 27, 31};
    testArray = Arrays.copyOf(testArray, testArray.length + 1);
    testArray[testArray.length - 1] = 67; 
    for(int i = 0; i < testArray.length; i++)
    {
      System.out.println(testArray[i]);
    }
  }
}

The first line of code is the importing of java.util.Arrays. I then created an integer test array with three values. I utilized the Arrays.copyOf method to lengthen the array. The first part of the brackets is the array I’m adding to and the second is the new size. Since I just wanted to append a single value to the array, I used the original length and added one. The testArray was set to the result of that method, so it acts like adding a single space to the original array. I then added the new value to the end of the array. Since the indexes of arrays start at zero, I subtracted one from the length of the array when deciding where to place the value. To confirm that everything worked, I printed out the entire array.

This method is much more efficient compared to the first method and it looks much more clean. If I were to add to an array, I would use this method over the previous one.

Visual

Method 3:

https://programming.guide/java/array-append.html

https://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html

https://simplesolution.dev/java-insert-element-into-an-array-at-index-using-apache-commons-lang/

This method uses the ArrayUtils.add method to append to an array. It requires the Apache Commons Lang API. The set up seems complicated at first, but the method used is very short and simple. This requires adding a package, luckily Replit makes this easy.

First, I needed to open the packages tab to search for my package.

I then searched for the package I needed (org.apache.commons:commons-lang3).

I installed the package and waited for the build to finish.

Finally, I imported the ArrayUtils that I needed.

With all of that done, I was able to finally write the rest of my code.

Example Code

import org.apache.commons.lang3.ArrayUtils;

class Main {
  public static void main(String[] args) {
    int[] testArray = {42, 27, 31};
    testArray = ArrayUtils.add(testArray, 67);
    for(int i = 0; i < testArray.length; i++)
    {
      System.out.println(testArray[i]);
    }
  }
}

As mentioned earlier, I imported ArrayUtils. After that, I created an integer testArray with three int values. The next line utilizes the ArrayUtils.add method to append the number 67 into the array. The first part of the method is the array which you are adding to and the second is what you are adding. Since testArray is being set to the result of the method, it just acts as appending to the array. I then printed all of the numbers inside the testArray to ensure that the appending worked.

This method is slightly shorter than the previous, but requires much more initial set up. I may go this way if I had to add to an array. All of the set up wasn’t just for the ArrayUtils. The package has many more methods that I could import and utilize to do other things. So, if I had already added the package and needed to add to an array, I would use this one over the previous.

Visual