JetBrains Academy: ArrayList - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Creating list:
Create ArrayList by name list
in any way known to you and fill it with the following four numbers 2, 0, 1, 7
.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(4);
list.add(2);
list.add(0);
list.add(1);
list.add(7);
System.out.println(list);
}
}
Output the list's elements:
The list of strings is given.
ArrayList<String> nameList = new ArrayList<>(Arrays.asList("Mr.Green", "Mr.Yellow", "Mr.Red"));
Output each its element in the loop. Each element must be in new line.
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<String> nameList = new ArrayList<>(Arrays.asList("Mr.Green", "Mr.Yellow", "Mr.Red"));
for (String name : nameList){
System.out.println(name);
}
}
}
Concat positive numbers:
Implement a method to concatenate all positive numbers of two input ArrayList
's to a single result list.
The first part of the result list must be filled with elements from the first ArrayList
, and the second part with elements from the second ArrayList
.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
class ConcatPositiveNumbers {
public static List<Integer> concatPositiveNumbers(List<Integer> l1, List<Integer> l2) {
List<Integer> positiveNumbers = new ArrayList<>();
for (Integer number : l1){
if (number > 0){
positiveNumbers.add(number);
}
}
for (Integer number : l2){
if (number > 0){
positiveNumbers.add(number);
}
}
return positiveNumbers;
}
/* Do not modify this method */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> list1 = readArrayList(scanner);
List<Integer> list2 = readArrayList(scanner);
List<Integer> result = concatPositiveNumbers(list1, list2);
result.forEach((n) -> System.out.print(n + " "));
}
/* Do not modify this method */
private static List<Integer> readArrayList(Scanner scanner) {
return Arrays
.stream(scanner.nextLine().split("\\s+"))
.map(Integer::parseInt)
.collect(Collectors.toCollection(ArrayList::new));
}
}
Find the nearest number:
Given a sequence of integers, and a number N, find out the closest integer from the array to the N. If you find several integers with the same distance to the N, you should output all of them in the ascending order.
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> list1 = readArrayList(scanner);
int n = Integer.parseInt(scanner.nextLine());
List<Integer> result = nearestNumbers(list1, n);
result.forEach((number) -> System.out.print(number + " "));
}
private static List<Integer> nearestNumbers(List<Integer> list, Integer n) {
List<Integer> differences = new ArrayList<>();
int smallestDifference = Integer.MAX_VALUE;
List<Integer> nearest = new ArrayList<>();
for (Integer number : list){
differences.add(Math.abs(number - n));
}
for (Integer elem : differences) {
smallestDifference = elem < smallestDifference ? elem : smallestDifference;
}
for (int i = 0; i < differences.size(); i++){
if (differences.get(i) == smallestDifference){
nearest.add(list.get(i));
}
}
Collections.sort(nearest);
return nearest;
}
private static List<Integer> readArrayList(Scanner scanner) {
List<String> list = Arrays.asList(scanner.nextLine().split("\\s+"));
List<Integer> numbers = new ArrayList<>();
list.forEach((elem) -> numbers.add(Integer.parseInt(elem)));
return numbers;
}
}