desislava_topuzakova

3. Списък с продукти

Feb 27th, 2022
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. package Lists;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class ListOfProducts_03 {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         int n = Integer.parseInt(scanner.nextLine()); //общ брой продукти
  12.         List<String> products = new ArrayList<>(); //празен списък с текстове
  13.         for (int product = 1; product <=  n; product++) {
  14.             String productName = scanner.nextLine();
  15.             //1. добавя в списъка с продукти
  16.             products.add(productName);
  17.         }
  18.  
  19.         //списък с продукти -> сортирам, принтирам
  20.         //{4, 76, 13 , 56} -> възходящ ред -> {4, 13, 56, 76}
  21.         //{4, 76, 13 , 56} -> низходящ ред-> {76, 56, 13, 4}
  22.         //{"zebra", "crocodile", "apple", "smart"} -> възходящ ред (ascending) (a -> z) -> {"apple", "crocodile" ,"smart", "zebra"}
  23.         //{"zebra", "crocodile", "apple", "smart"} -> низходящ ред (descending) (z -> a) -> {"zebra", "smart", "crocodile", "apple"}
  24.  
  25.         //сортиране
  26.         Collections.sort(products); //ascending (възходящ ред)
  27.  
  28.         //принтираме:
  29.         int number = 1;
  30.         for (String product : products) {
  31.             System.out.println(number + "." + product);
  32.             number++;
  33.         }
  34.  
  35.  
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment