Commit 71eae2ad by Patryk Czarnik

ZbieranieDanych2_Lista

parent 89c1bdb8
package p20_zbieranie_danych;
import java.util.*;
public class ZbieranieDanych2_Lista {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<String> lista = new ArrayList<>();
System.out.println("Aby zakończyć pobieranie danych, wpisz pusty napis");
for(int i = 0; ; i++) {
System.out.print("Podaj wartość nr " + i + ": ");
String s = scanner.nextLine();
if(s.isEmpty()) break;
lista.add(s);
}
System.out.println(lista);
// Listę da się posortować
// Collections.sort(lista);
lista.sort(null);
System.out.println(lista);
System.out.println();
System.out.println("Podawaj numery pozycji, aby je odczytać, a -1 aby zakończyć program.");
while(true) {
System.out.print("Podaj numer: ");
int nr = scanner.nextInt();
if(nr < 0) break;
if(nr >= lista.size()) {
System.out.println("Numer poza listą");
continue;
}
System.out.println("Na pozycji nr " + nr + " jest " + lista.get(nr));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment