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( double[] arr, int k) {
// To do 5. Complete this method
}
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:
Java 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(...
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 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 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[])...
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)...
The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...
Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods. Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement Element to be found */ int findAll(int...
the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...
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]...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...