12

Let's that I have a number N. N will be the size of the array.

int numArray [] = new numArray[N];

However, the contents of the array will hold every other number from 1 to positive N. This means that the entire size N array will not be full after that for loop. So after the for loop, I want to trim (or resize) the array so that there will no longer be any empty slots in the array.

Example :

Let's say N = 5; That means, after the for loop, every other number from 1 to 5 will be in the array like so:

int arr[] = new int[N];

int arr[0]=1;
int arr[1]=3;
int arr[2]= null;
int arr[3]= null;
int arr[4]= null;

Now, I want to trim (or resize) after the for loop so that the indexes that hold null will be gone and then the array should be:

int arr[0]=1;
int arr[1]=3;

The size of the array is now 2.

Ebad Saghar
  • 1,107
  • 2
  • 16
  • 41
  • Arrays have a fixed size so you may want to look at alternative data structures, such as an arraylist. – sager89 Mar 23 '14 at 04:28
  • Check out this question: http://stackoverflow.com/questions/12002264/removing-unfilled-values-or-null-values-from-array-of-string-in-java – jlewkovich Mar 23 '14 at 04:31

5 Answers5

16

You can't trim an array. The fastest approach is just to copy it into a smaller one, using System.arraycopy, which is almost always much faster than a for loop:

int somesize = 5;
int[] numArray = new int[somesize];
//code to populate every other int into the array.
int[] smallerArray = new int[somesize/2];
//copy array into smaller version
System.arraycopy(numArray, 0, smallerArray, 0, somesize / 2);
Community
  • 1
  • 1
lreeder
  • 12,047
  • 2
  • 56
  • 65
  • 1
    why divide by two ? I don't want to divide by 2. N can be any size, not just 5. Please explain the division. – Ebad Saghar Mar 23 '14 at 04:35
  • If your primary array is size N, and it's only populated with every other number, the number of non-null values in your primary array will have size N/2 – lreeder Mar 23 '14 at 04:36
  • 1
    Ah, I see. Very simple thinking there that I didn't catch. – Ebad Saghar Mar 23 '14 at 04:37
13

You can't change the size of an array in Java after it has been created. What you can do however, is to create a new array of the size that you need.

Another important point is that you are creating an array of a primitive: int. Primitives are not objects and you cannot assign the value null to a primitive. You need to create an array of java.lang.Integer if you want to be able to set entries in it to null.

Integer[] numArray = new Integer[N];

Thanks to a Java feature called auto-boxing, almost all code that works with primitive int values, also works with Integer values.

Steps:

  1. Use Integer[] instead of int[]
  2. Calculate the size that you need (count non-null entries in original array)
  3. Allocate a new array of the size that you need
  4. Loop over the old array, and copy every non-null value from it to the new array.

Code:

Integer[] oldArray = ...;

// Step 2
int count = 0;
for (Integer i : oldArray) {
    if (i != null) {
        count++;
    }
}

// Step 3
Integer[] newArray = new Integer[count];

// Step 4
int index = 0;
for (Integer i : oldArray) {
    if (i != null) {
        newArray[index++] = i;
    }
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
10

I think there is a bit shorter way to do the trimming itself. Whats left is to find the proper index.

You can do:

int someIndex = Arrays.asList(arr).indexOf(null);
arr = Arrays.copyOfRange(arr,0,someIndex);
promanowicz
  • 399
  • 2
  • 10
1

You surely better of with some more appropriate data structure, for example a list or a set depending on what's your intention with it later. That way you don't even need to create an N sized structure just so you'd have to reduce it anyway. Rather you create an empty list and add the elements that you actually need

fejese
  • 4,601
  • 4
  • 29
  • 36
1
import java.util.Arrays;  

public static void main( String[] args )
    {
        int[] nums2 = {9,4,1,8,4};

        nums2 =Arrays.copyOf(nums2,3);

        for (int i : nums2) {
            System.out.print(i+" ");
        }


    }

//Output

9 4 1

Lijo
  • 6,498
  • 5
  • 49
  • 60