13

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.

2
  • Arrays have a fixed size so you may want to look at alternative data structures, such as an arraylist. Commented Mar 23, 2014 at 4:28
  • Check out this question: stackoverflow.com/questions/12002264/… Commented Mar 23, 2014 at 4:31

5 Answers 5

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); 
Sign up to request clarification or add additional context in comments.

3 Comments

why divide by two ? I don't want to divide by 2. N can be any size, not just 5. Please explain the division.
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
Ah, I see. Very simple thinking there that I didn't catch.
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; } } 

Comments

12

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); 

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.