Results 1 to 5 of 5
  1. #1
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Exclamation 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.

    Java Code:
    import java.io.*;import java.util.*;
    import java.util.Arrays.*;
    import java.lang.*;

    Java 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*/

    Java 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
    Java 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
    Java 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
    Last edited by 765891; 12-04-2013 at 06:10 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,661
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default 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...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Default Re: Removing 0's from 1D int array

    Resolved everything, cheers to gimbal for making me think, thanks all
    Last edited by 765891; 12-05-2013 at 08:15 AM.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Removing components from an array
    By shivam0101 in forum New To Java
    Replies: 5
    Last Post: 09-20-2012, 11:56 PM
  2. Removing Duplicates in an Array
    By jerseyjava in forum New To Java
    Replies: 7
    Last Post: 02-01-2012, 07:46 PM
  3. Removing characters in an array
    By dru36 in forum New To Java
    Replies: 6
    Last Post: 11-27-2011, 12:56 PM
  4. Removing indexes from an array.
    By Cruncher in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 09:41 PM
  5. removing 0 from the begining of the array
    By Hayzam in forum New To Java
    Replies: 7
    Last Post: 08-21-2008, 10:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •