Untitled
unknown
java
5 months ago
2.3 kB
4
Indexable
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * 1. Parašykite statinį metodą masyvas(), kuris priima bet kokio ilgio sveikųjų skaičių masyvą a[], sudarytą iš atsitiktinių skaičių nuo 0 iki 9 (tam galite panaudoti random() metodą), * ir grąžina masyvą b[], kurio i-tasis elementas lygus pasirodymui skaičiaus i masyve a[]. Išveskite masyvą a[] ir masyvą b[]. * Pvz.: * 2 4 2 5 9 5 7 4 5 8 9 2 3 4 0 6 2 4 0 2 8 4 7 1 1 3 9 6 5 3 * ----------------------------------------------- * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | * ----------------------------------------------- * | 2 | 2 | 5 | 3 | 5 | 4 | 2 | 2 | 2 | 3 | * ----------------------------------------------- */ public class MainU1 { public static void main(String[] args) { List<Integer> a = new ArrayList<Integer>(); for (int i = 0; i < 30; i++) { a.add(rnd(0, 9)); } List<Integer> b = masyvas(a); //Print random numbers System.out.println(a.stream().map(Object::toString).collect(Collectors.joining(" "))); printTable(b); } public static List<Integer> masyvas(List<Integer> a) { List<Integer> b = new ArrayList<Integer>(); //fill array for (int i = 0; i <= 9; i++) { b.add(i, 0); } a.forEach((number) -> { b.set(number, b.get(number)+1); }); return b; } public static int rnd(int Min, int Max) { return Min + (int)(Math.random() * ((Max - Min) + 1)); } public static void printTable(List<Integer> b) { System.out.printf("-----------------------------------------%n"); System.out.printf("| %-1s | %-1s | %-1s | %-1s | %-1s | %-1s | %-1s | %-1s | %-1s | %-1s |%n", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); System.out.printf("-----------------------------------------%n"); System.out.printf("| %-1s | %-1s | %-1s | %-1s | %-1s | %-1s | %-1s | %-1s | %-1s | %-1s |%n", b.get(0), b.get(1), b.get(2), b.get(3), b.get(4), b.get(5), b.get(6), b.get(7), b.get(8), b.get(9)); System.out.printf("-----------------------------------------%n"); } }
Editor is loading...
Leave a Comment