Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim out an array of integers in Java?

Tags:

java

arrays

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.

like image 414
Ebad Saghar Avatar asked Mar 23 '14 04:03

Ebad Saghar


5 Answers

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;
    }
}
like image 86
Erwin Bolwidt Avatar answered Oct 18 '22 05:10

Erwin Bolwidt


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);
like image 23
promanowicz Avatar answered Oct 18 '22 07:10

promanowicz


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);
like image 15
lreeder Avatar answered Oct 18 '22 07:10

lreeder


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

like image 1
fejese Avatar answered Oct 18 '22 05:10

fejese


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

like image 1
Lijo Avatar answered Oct 18 '22 07:10

Lijo