Question

ava deleting from an array // arrayDelete        //        // delete the value...

ava deleting from an array

// arrayDelete

       //

       // delete the value in the array at position k

       // positions are numbered starting with 0

       // elements k+1 through N-1 are moved 'down' one position

       // to close the space left by deleting element k

       // set element N-1 to -99

       // preconditions: 0 <= k <= N-1

       //

   public static void arrayDelete( double[] arr, int k) {

      

       // To do 5. Complete this method

   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class ArrayDelete {
   /*Array is initialized as static so that it can be accessed by both the main method and delete method*/
   static double[] array = new double[] {1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9};
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       //Printing the initial Array elements
       System.out.println("Initial array: ");
       for(double i:array){
           System.out.print(i+", ");
       }
       System.out.println();
       //Deleting the element which is in position 2 i.e., 0,1,2
       int k= 2;
       //Callint the araayDelete function to delete the element in positin k
       arrayDelete(array, k);
       //Printing the array after removing the elements
       System.out.println("Array After deleting an element: ");
       for(int i=0; i<array.length-1; i++){
           System.out.print(array[i]+", ");
       }
   }

   public static void arrayDelete(double[] array, int k) {
       // TODO Auto-generated method stub
       /* The following loop also works fine just in case if predefined function is not supposed to be used*/
       /*for(int i=0; i<array.length; i++){
           if (k==i){
               for(int j= i+1; j<array.length; j++){
                   array[i]= array[j];
                   i++;
               }
           }
       }*/
       System.arraycopy(array,k+1,array,k,array.length-1-k);
       //N-1 element is initialized to -99 as per the question
       array[array.length-1]=-99;
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
ava deleting from an array // arrayDelete        //        // delete the value...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT