Explanation of Solution
Adding the “lastIndexOf” methods in “ArrayIntList” class:
//definition of "ArrayIntList" class
public class ArrayIntList
{
//declare the required variables
private int[] elementData;
private int size;
//refer the remaining methods in the textbook
//definition of "lastIndexOf" method
public int lastIndexOf(int value)
{
/*iterate "i" until it reaches the size of an array*/
for (int i = size - 1; i >= 0; i--)
{
//check the condition
if (elementData[i] == value)
{
//return the "i" value
return i;
}
}
//return the value
return -1;
}
}
Explanation:
In the above program, the “ArrayIntList” is the class name,
- Inside the “ArrayIntList” class, declare the “elementData”, and “size” variables as a private integer datatype.
- The “lastIndexOf” method is defined inside the “ArrayIntList” class with the integer value as the parameter.
- The “for” loop is used to iterate the value until it reaches size of an array.
- The “if” loop is used to check whether the array of element value is equal to passed value.
- The “ith” position of an array value is returned.
- The “if” loop is used to check whether the array of element value is equal to passed value.
- If the value is not in an array return the -1 value.
- The “for” loop is used to iterate the value until it reaches size of an array.
- The “lastIndexOf” method is defined inside the “ArrayIntList” class with the integer value as the parameter.
Want to see more full solutions like this?
Chapter 15 Solutions
Building Java Programs: A Back To Basics Approach Plus Mylab Programming With Pearson Etext -- Access Card Package (5th Edition)
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education