/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication19;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class JavaApplication19 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
{
ArrayList<Integer> list1 = new ArrayList<>();
// Generate 5 random integers and add them to the list
Random random = new Random();
for (int i = 0; i < 5; i++) {
list1.add(random.nextInt(100) + 1);
}
System.out.print("5 random integers: ");
for (int i : list1) {
System.out.print(i + " ");
}
System.out.println();
// Sort the list in ascending order
Collections.sort(list1);
// Print the 2nd largest number
System.out.println("2nd largest number is " + list1.get(list1.size() - 2));
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication21;
import java.util.Random;
/**
*
* @author laiya
*/
public class JavaApplication21 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[] A = new int[10];
Random random = new Random();
for (int i = 0; i < 10; i++) {
A[i] = random.nextInt(6) + 1;
}
for (int value : A) {
System.out.print(value + " ");
}
System.out.println();
for (int i = 0; i < 10; i++) {
if (i < 9 && A[i] == A[i + 1]) {
System.out.print("(");
while (i < 9 && A[i] == A[i + 1]) {
System.out.print(A[i] + " ");
i++;
}
System.out.print(A[i] + ") ");
}
else{
System.out.print(A[i] + " ");
}
}
}
}