Question

JAVA write a completed program (must include MAIN) and straight to the output on Q1

e following exercises is a method to be added to the ArrayIntlist class from this chapter Each of the a method called lastInd

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class ArrayIntList {
  public static int lastIndexOf(int arr[], int key){
    int res = -1;
    for(int i = 0;i<arr.length;i++){
      if(arr[i] == key){
        res = i;
      }
    }
    return res;
  }

  public static void main(String args[]){
    int arr1[] = {1,18,2,7,18,39,18,40};
    System.out.println(lastIndexOf(arr1, 18));
    System.out.println(lastIndexOf(arr1, 3));
  }
}

Output:

6

-1

Please up vote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
JAVA write a completed program (must include MAIN) and straight to the output on Q1 e...
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