I have started a 30-day Selenium with Java challenge to help beginners and intermediate software testers for learning Selenium and Java concepts. This is first challenge. Share your answers in the comments about the program’s output—I’ll provide my short answer afterward.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.testng.annotations.Test;
public class SampleProgram1 {
@Test
public void testMySkills() {
System.out.println(getListItem());
}
public int getListItem() {
List<Integer> list = new ArrayList<>();
list.add(8);
list.add(9);
list.add(6);
int i = 0;
for (int num : list) {
list.set(i, num + 10);
i++;
}
Collections.sort(list);
return list.get(0);
}
}
Hi Viewer, as part of the program we have added values 8, 9 and 6 to the list and loop through the list and added 10 to each value. later we have sorted the list using Collections.sort and returned 0 index value hence the program outputs 16.
initial list=[8, 9, 6]
Adding 10 to each element in the list
new list=[18,19,16]
After sorting
list=[16,18,19]
The output is the first element from the list=16.
Hi Princy,
It is nice explanation!
Thanks