# Removing 0's from 1D int array

Printable View

• 12-04-2013, 07:08 AM
765891
Removing 0's from 1D int array
Hello and Thank you for your time and patience for helping me!
I need help removing the zeros from an int array that has 20 random gen ints with 25 elements I need help shortening an array. Please resort to 1 Dimensional arrays if possible. Arraylists should be avoided in the solution code if possible, regular Int array is recomended . Thank you so much for time and help. elements of the array CANNOT BE HARDCODED, so I need help removing the zeros in the middle and the end, need help shortening the array if possible.

Code:

```import java.io.*;import java.util.*; import java.util.Arrays.*; import java.lang.*;```

Code:

```/* Fills the numbers array with 20 random integers      precondition: None       postcondition: numElementsCopy contains physical size of array       @return the number of elements(physical size) that have been copied to the       numbers array*/```

Code:

```    public static int fillArray(int[] numbersReference)    {         int numElementsCopy = 0;                 java.util.Random r = new java.util.Random();                 for(int k = 0; k < 20; k++)         {                         if(k%7 == 0)             {                 numbersReference[k] = 0;                 numElementsCopy++;             }                else             {                 numbersReference[k] = r.nextInt(100);                 numElementsCopy++;                }         }                 return numElementsCopy;     }```
//Failed attempt one
Code:

```int count=0;        int [] numbersReference2;         for(int i=0;i<numbersReference.length;i++)         {             while(numbersReference[numbersReference.length-1]==0)             {                 count++;                 numbersReference2 =new int[numbersReference.length-count];             }                             if(numbersReference[i]==0)             {                 count++;                 numbersReference2 =new int[numbersReference.length-count];                 numbersReference2[i]=numbersReference[i+1];                 i++;             }         }```

//failed attempt twp
Code:

```    static void RemoveZeros(int[] numbersReference )    {         int [] arraydupe=numbersReference;         int j = 0;         for( int i=0;  i<arraydupe.length;  i++ )         {             if (arraydupe[i] != 0)                 arraydupe[j++] = arraydupe[i];         }         numbersReference = new int[j];         System.arrayCopy( arraydupe, 0, numbersReference, 0, j );                        }     // shifts all array elements one cell to the left,     // losing the value in the zeroth cell and decreasing the number     // of elements in the array by 1```
• 12-04-2013, 11:13 AM
JosAH
Re: Removing 0's from 1D int array
Why do you want to stick to arrays? Because the assignent said so? With a List<Integer> the problem is trivial, but it is a nasty little thing to solve with arrays (it still can be done though) ...

kind regards,

Jos
• 12-04-2013, 11:18 AM
gimbal2
Re: Removing 0's from 1D int array
"Removal" is such an open ended term. You might also describe it as "leaving it out", say during a copy...
• 12-05-2013, 03:24 AM
765891
Re: Removing 0's from 1D int array
Resolved everything, cheers to gimbal for making me think, thanks all
• 12-05-2013, 09:53 AM
gimbal2
Re: Removing 0's from 1D int array
Good work! If you're looking for some suggestions on ways to make improvements, feel free to share the code.