182k views
3 votes
Write a method named lastIndexOf that accepts an array of integers and an * integer value as its parameters and returns the last index at which the * value occurs in the array. The method should return -1 if the value is not * found.

1 Answer

4 votes

Answer:

The method is as follows:

public static int lastIndexOf(int [] numbers, int num){

int lastIndex = numbers[0];

for(int i =0; i<numbers.length;i+=num){

lastIndex = numbers[i];

}

return lastIndex;

}

Step-by-step explanation:

This defines the method

public static int lastIndexOf(int [] numbers, int num){

This initializes the lastIndex to the first element of the array

int lastIndex = numbers[0];

This iterates through every "num" element of the array

for(int i =0; i<numbers.length;i+=num){

This gets the current index... until the last

lastIndex = numbers[i]; }

This returns the last

return lastIndex; }

User Till
by
5.1k points