Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. LinkedList<String> friends = new LinkedList<>();
  8.  
  9. // Dodawanie elementów za pomocą metody add
  10. friends.add("Andrzej");
  11. friends.add("Marek");
  12. friends.add("Janusz");
  13. friends.add("Dawid");
  14. friends.add("Marcin");
  15. friends.add("Edyta");
  16.  
  17. System.out.println("Lista przyjaciół : " + friends);
  18.  
  19. // Dodanie do listy nowy element na pozycji 3
  20. friends.add(3, "Kinga");
  21. System.out.println("Po dodaniu add(3, \"Kinga\") : " + friends);
  22.  
  23. // Dodanie na pierwszym miejscu nowego elementu
  24. friends.addFirst("Mariusz");
  25. System.out.println("Po metodzie addFirst(\"Mariusz\") : " + friends);
  26.  
  27.  
  28. // Dodanie wszystkich elementów na koniec listy LinkedList
  29. List<String> familyFriends = new ArrayList<>();
  30. familyFriends.add("Jesse");
  31. familyFriends.add("Walt");
  32.  
  33. friends.addAll(familyFriends);
  34. System.out.println("Po metodzie addAll(familyFriends) : " + friends);
  35.  
  36.  
  37. System.out.println("Lista początkowa = " + friends);
  38.  
  39. // Usuwanie pierwszego elementu z tablicy
  40. String element = friends.removeFirst();
  41. System.out.println("Usunięty elemenet " + element + " => " + friends);
  42.  
  43. // RUsuwanie ostatniego eementu w tablicy
  44. element = friends.removeLast();
  45. System.out.println("Usunięty ostatni element to " + element + " => " + friends);
  46.  
  47. // Usunięcie pirwszego elementu pasującego do wzorca
  48. boolean isRemoved = friends.remove("");
  49. if(isRemoved) {
  50. System.out.println("Usunięto C# => " + friends);
  51. }
  52.  
  53. // Usunięcie wszystkich elementów zaczynających się na literę M, wykorzystano tutaj predykat
  54. friends.removeIf(programmingLanguage -> programmingLanguage.startsWith("M"));
  55. System.out.println("Usunięto elementy zaczynające się na M => " + friends);
  56.  
  57. // Usunięcie wszystkich elementów
  58. friends.clear();
  59. System.out.println("Wyczyszczenie tablicy => " + friends);
  60.  
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement