Finding Largest,Second Largest,Lowest Number in ArrayList




Example:

package largestnumber;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Scanner;

import org.apache.commons.collections.IteratorUtils;

public class highestnumber {

public static void main(String[] args) {
int i = 0;

ArrayList<Integer> arraylist = new ArrayList<>();
Scanner s = new Scanner(System.in);
System.out.println("Enter size of array:");
int n = s.nextInt();

System.out.println("You have to enter  " + (n - i) + " values:");
for (; i < n; i++)
 {
arraylist.add(s.nextInt());

}

Collections.sort(arraylist);
System.out.println("All Elements in the Array List are: " +arraylist);
System.out.println("Lowest Number in the  Array is:"
+ arraylist.get(0));
System.out.println("Second Largest Number in the Array is: "
+ arraylist.get(arraylist.size() - 2));
System.out.println("Largest Number in the Array is: "
+ arraylist.get(arraylist.size() - 1));

}

}


Output:

Enter size of array:
5
You have to enter  5 values:
3452
7865
9783
1748
9934

All Elements in the Array List are: [1748, 3452, 7865, 9783, 9934]
Lowest Number in the  Array is:1748
Second Largest Number in the Array is: 9783
Largest Number in the Array is: 9934


No comments:

Post a Comment