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
  • Java deleting from an array // arrayDelete        //        // delete the value...

    Java 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(...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • pls help, idk whats wrong with this Add the reverse() method which reverses the content of...

    pls help, idk whats wrong with this Add the reverse() method which reverses the content of array without using additional array and the rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However,...

    I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?: public class CharArrayProject_3 { private char[] array; privateint length; privateintnumberOfRepeats; public CharArrayProject_3( char[] arr ) { length = arr.length; array = new char[ length ]; numberOfRepeats = 0; for( int k = 0; k < arr.length; k++ ) { array[k]...

  • 2. Use hashing (solve_with_Hash(int[] array, int k)) Initialize a counter variable to O: Insert a...

    2. Use hashing (solve_with_Hash(int[] array, int k)) Initialize a counter variable to O: Insert all elements of array in a hashtable For every element in array: counter0 a. b. c. .Look for array[i] . Look for array [幻 + k in the hash map, if found then increment counter. -k in the hash map, if found then increment counter. Remove arrayli] from hash table. d. return counter For example: Input : array[] {1, 5, 3, 4, 2), k 3 Output:...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • (JAVA) Given an array of unique positive integers, write a function findSums that takes the array...

    (JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...

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