How to find the minimum value in the array list in Java
Boris~November 3, 2020 /Array List
To find the minimum value in the array in Java, you should follow several steps. The basic idea to set the temporary variable that we will use to compare with each element in the list. By default, the minimum element should be the first element in the list. With the help of the loop, we will compare the current value of the temporary variable with the value of the current element in the loop. As the result, If the temporary variable is less than the value from the loop, we need to update the temporary variable's value.
Search for a minimum element in the array list in Java example. Step-by-step implementation.
If you want to find the minimum element in the array list, you have to follow several steps:
Set the temporary variable. By default, the value should be the value of the first element in the array list.
//Creating the start point to compare it with the rest elements which is first element
int temporaryVariable = intArray[0];
Start the loop. Inside the loop, we need to compare the value of the temporary variable to each element's value.
//Going through the array and comparing each element and return the minimum one
for (int i = 0; i < arrayLength; i++) {
if (temporaryVariable > intArray[i]) {
Set the new value to the temporary variable. Because it supposes the temporary variable's value is less than the value of the current element in the loop after the comparison. We should update the temporary variable value.
The full code example:
private void findMinimumOfArray() {
//Created a new array of integers
int[] intArray = new int[]{14, 5, 35, 44, 5, 17, 78, 13, 9, 21};
//Get the length of the array
int arrayLength = intArray.length;
//If next element smaller than the existing we are setting it as a smallest element
temporaryVariable = intArray[i];
//Showing in logs the smallest element
Log.d("arrayList", "The minimum element is: " + temporaryVariable);
The full code schema:
In this article, you figured out the easiest way to find the minimum element in an array list in java.
Full code and more examples in my facebook group.
Search for an element in the Linked List in Java
Deleting an item in the Linked List in Java (Android Studio)
Adding items to the Linked List in Java.
Getting the length (size) of Linked List in Java (Android Studio)
Two ways to implement a Linked List in Java. Example.
Reversing the linked list.
Middle of the linked list example.
Find the nth element from the last in Linked List (Java)
Removing duplicates from the linked list
Inserting an item in the linked list (Java)