Question

please check first question before you do this one Java please it's data sturcture classs using java editor netbeans

2.  3. public static int indexOfLargest(int[] arr) – if arr is null then return -1; otherwise return the index of the largest val

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static int indexOfLargest(int[] arr){
    if(arr==null){
        return -1;
    }
    else{
        int largestIndex = 0;
        for(int i = 1;i<arr.length;i++){
            if(arr[largestIndex] < arr[i]){
                largestIndex = i;
            }
        }
        return largestIndex;
    }
}

public static int[] removeLargest(int[] arr){
    int largestIndex = indexOfLargest(arr);
    if(largestIndex!=-1){
        int res[] = new int[arr.length-1];
        System.arraycopy(res,0,arr,0,largestIndex);
        System.arraycopy(res,largestIndex,arr,largestIndex+1,res.length-largestIndex);
        return res;
    }
    else{
        return null;
    }
}
Add a comment
Know the answer?
Add Answer to:
please check first question before you do this one Java please it's data sturcture classs using...
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
  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • 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[])...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • 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)...

  • JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE....

    JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: ';' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: <identifier> expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:94: error: illegal start of type return true; ^ Project4.java:98: error: class, interface, or enum expected static int bSearch(int[] a, int count, int key) ^ Project4.java:101: error: class, interface, or...

  • JAVA: array return types May you please help me fix this coding. Sum is SUPPOSE to...

    JAVA: array return types May you please help me fix this coding. Sum is SUPPOSE to be 1253 import java.util.Scanner; public class Array { public static int sum(int[] arr) { int i; int total = 0; for ( i =0; i < arr.length; ++i) { total = total + arr[i]; } return total; } public static void main(String[] args) { int[] myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44,...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not...

    Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not use any Java language features we have not cover so far in this course. Assignment Objectives After completing this assignment the student should be able to:  Declare and instantiate arrays  Access array elements by index  Use loops and decisions to manipulate arrays and array elements  Write methods that manipulate arrays  Write methods that take array arguments  Write methods...

  • 1). The result of Java expression 5*7>3*(5+2) && 3*5-4<6 is ____ a. 35>35&&9<6 b. true &&...

    1). The result of Java expression 5*7>3*(5+2) && 3*5-4<6 is ____ a. 35>35&&9<6 b. true && false c. false d. False (2). You save text files containing Java language source code using the file extension___ a. .java b. .class c. .txt d. .src    (3). Which of the following is not a primitive data type in the Java programming language? a. boolean b. byte c. int d. Point (4). Given the Java statement int[][] vari = new int[5][7]; the value...

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