Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lists;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- public class ListOfProducts_03 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine()); //общ брой продукти
- List<String> products = new ArrayList<>(); //празен списък с текстове
- for (int product = 1; product <= n; product++) {
- String productName = scanner.nextLine();
- //1. добавя в списъка с продукти
- products.add(productName);
- }
- //списък с продукти -> сортирам, принтирам
- //{4, 76, 13 , 56} -> възходящ ред -> {4, 13, 56, 76}
- //{4, 76, 13 , 56} -> низходящ ред-> {76, 56, 13, 4}
- //{"zebra", "crocodile", "apple", "smart"} -> възходящ ред (ascending) (a -> z) -> {"apple", "crocodile" ,"smart", "zebra"}
- //{"zebra", "crocodile", "apple", "smart"} -> низходящ ред (descending) (z -> a) -> {"zebra", "smart", "crocodile", "apple"}
- //сортиране
- Collections.sort(products); //ascending (възходящ ред)
- //принтираме:
- int number = 1;
- for (String product : products) {
- System.out.println(number + "." + product);
- number++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment