Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import java.util.*;
  2. class JavaExample{
  3. public static void main(String args[]){
  4. ArrayList<String> abooks=new ArrayList<String>(); // Arraylist is dynamic vs Array
  5. ArrayList<String> books=new ArrayList<String>();
  6. abooks.add("Matthew");
  7. abooks.add("Mark");
  8. abooks.add("John");
  9.  
  10. //display Gospel books
  11. System.out.println("Titles of the Gospels using an ArrayList:");
  12. for (String str:abooks)
  13. System.out.println(str);
  14. System.out.println();
  15.  
  16. abooks.add(2,"Luke"); // inserting a skiped book
  17. abooks.add(4,"Luke"); // inserting a skiped book - duplicate
  18.  
  19. //display Gospel books
  20. System.out.println("Titles of the Gospels after adding missed book:");
  21. for (String str:abooks)
  22. System.out.println(str);
  23. System.out.println();
  24.  
  25. abooks.remove(4); // removing duplicate entry
  26.  
  27. //display Gospel books
  28. System.out.println("Titles of the Gospels after removing duplicate:");
  29. for (String str:abooks)
  30. System.out.println(str);
  31. System.out.println();
  32.  
  33. //Sorting Asending Order
  34. System.out.println("sorting alphabetically");
  35. Collections.sort(abooks);
  36. for (String str:abooks)
  37. System.out.println(str);
  38. System.out.println();
  39.  
  40. // Adding book of Acts
  41. Collections.sort(abooks,Collections.reverseOrder());
  42. abooks.add(4,"Acts");
  43.  
  44. System.out.println("Titles After adding Acts and resorting:");
  45. for (String str:abooks)
  46. System.out.println(str);
  47. System.out.println();
  48. System.out.println();
  49.  
  50. // creating a linked list
  51. LinkedList<String> gospellist2=new LinkedList<String>();
  52. System.out.println("-------------------");
  53. System.out.println("Same books using a linked list");
  54. System.out.println("----------");
  55.  
  56. //Adding elements to the Linked list
  57. gospellist2.add("Mark");
  58. gospellist2.add("John");
  59.  
  60. System.out.println("starting list");
  61. System.out.println("----------");
  62. System.out.println(gospellist2);
  63.  
  64. System.out.println("Adding Matthew and Luke");
  65. System.out.println("----------");
  66.  
  67. //Adding an element to the first position
  68. gospellist2.addFirst("Matthew");
  69. gospellist2.add(2, "Luke");
  70. System.out.println(gospellist2);
  71.  
  72. //Adding an element to the last position
  73. gospellist2.addLast("Acts");
  74.  
  75. //Iterating LinkedList
  76. System.out.println("New list of books with an iterator:");
  77. System.out.println("----------");
  78. Iterator<String> iterator=gospellist2.iterator();
  79. while(iterator.hasNext()){
  80. System.out.println(iterator.next());
  81. }
  82. //Removing Acts
  83. gospellist2.removeLast();
  84.  
  85. /*This is how to get and set Values*/
  86. Object firstvar = gospellist2.get(0);
  87. System.out.println("First element: " +firstvar);
  88. System.out.println();
  89. gospellist2.set(0, "New Testiment");
  90. Object firstvar2 = gospellist2.get(0);
  91. System.out.println("First element after update by get and set method: " +firstvar2);
  92. System.out.println();
  93. System.out.println("new list of elements after update by set method: " +gospellist2);
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement