I need help with this assignment please explain in short details about what each
ID: 3568148 • Letter: I
Question
I need help with this assignment please explain in short details about what each method does to get the result
Write a method called min that returns the minimum value in a list of integers. If the list is empty, it should throw a NoSuchElementException. Write a method called isSorted that returns true if the list is in sorted (nondecreasing) order and returns false otherwise. An empty list is considered to be sorted. Write a method called lastIndexOf that accepts an integer value as a parameter and that returns the index in the list of the last occurrence of that value, or -1 if the value is not found in the list. For example, if a variable list stores the values [1, 18, 2, 7, 18, 39, 18, 40] Then the call of list .lastIndexOf(18) should return 6. If the call has instead been list.lastIndexOf(3), the method would return -1.Explanation / Answer
1)
if(array!=empty){}
throw new IllegalArgumentException("The filename cannot be null!");
}
else{}
minimum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
}
2)
public static boolean isSorted(int[] list) {
// sort list from min to max
for (int i = 0; i < list.length; i++) {
if (list[i] > list[i + 1])
return false;
i++;// plus i/isSorted = true will be out of bounds
}
return true;
}
3)
ArrayList<String> arrlist = new ArrayList<String>(5);
// use add() method to add values in the list
arrlist.add("G");
arrlist.add("E");
arrlist.add("F");
arrlist.add("M");
arrlist.add("E");
System.out.println("Size of list: " + arrlist.size());
// let us print all the values available in list
for (String value : arrlist) {
System.out.println("Value = " + value);
}
// Retrieving the index of last occurrence of element "E"
int retval=arrlist.lastIndexOf("E");