Advertisement
Kotuara

Практика5.2

Dec 22nd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.stream.Stream;
  7.  
  8. public class Main {
  9.  
  10.     public static class HeavyBox
  11.     {
  12.         public String id;
  13.         public int weight;
  14.  
  15.         public HeavyBox(String id, int weight)
  16.         {
  17.             this.id = id;
  18.             this.weight = weight;
  19.         }
  20.  
  21.         public void Print()
  22.         {
  23.             System.out.println(id);
  24.             System.out.println(weight + "\n");
  25.         }
  26.     }
  27.  
  28.     public static void main(String[] args)
  29.     {
  30.         HeavyBox Box1 = new HeavyBox("Тяжелый", 789);
  31.         HeavyBox Box2 = new HeavyBox("Средний", 237);
  32.         HeavyBox Box3 = new HeavyBox("Легкий", 34);
  33.         ArrayList<HeavyBox> Warehouse = new ArrayList<>(Arrays.asList(Box1, Box2, Box3));
  34.         Iterator iterator = Warehouse.iterator();
  35.         Stream<HeavyBox> Warehouse1 = Stream.of(Box1, Box2, Box3);
  36.  
  37.         System.out.println("Способ 1:");
  38.         for (HeavyBox Box : Warehouse) {
  39.             Box.Print();
  40.         }
  41.  
  42.         System.out.println("Способ 2:"); //не понял, как вывести непосредственно значения объектов
  43.         while (iterator.hasNext()) {
  44.             System.out.println(iterator.next());
  45.         }
  46.  
  47.         System.out.println("\nСпособ 3:");
  48.         Warehouse1.forEach(element -> System.out.println(element.id+ " " +element.weight));
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement