Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class JavaExample{
- public static void main(String args[]){
- ArrayList<String> abooks=new ArrayList<String>(); // Arraylist is dynamic vs Array
- ArrayList<String> books=new ArrayList<String>();
- abooks.add("Matthew");
- abooks.add("Mark");
- abooks.add("John");
- //display Gospel books
- System.out.println("Titles of the Gospels using an ArrayList:");
- for (String str:abooks)
- System.out.println(str);
- System.out.println();
- abooks.add(2,"Luke"); // inserting a skiped book
- abooks.add(4,"Luke"); // inserting a skiped book - duplicate
- //display Gospel books
- System.out.println("Titles of the Gospels after adding missed book:");
- for (String str:abooks)
- System.out.println(str);
- System.out.println();
- abooks.remove(4); // removing duplicate entry
- //display Gospel books
- System.out.println("Titles of the Gospels after removing duplicate:");
- for (String str:abooks)
- System.out.println(str);
- System.out.println();
- //Sorting Asending Order
- System.out.println("sorting alphabetically");
- Collections.sort(abooks);
- for (String str:abooks)
- System.out.println(str);
- System.out.println();
- // Adding book of Acts
- Collections.sort(abooks,Collections.reverseOrder());
- abooks.add(4,"Acts");
- System.out.println("Titles After adding Acts and resorting:");
- for (String str:abooks)
- System.out.println(str);
- System.out.println();
- System.out.println();
- // creating a linked list
- LinkedList<String> gospellist2=new LinkedList<String>();
- System.out.println("-------------------");
- System.out.println("Same books using a linked list");
- System.out.println("----------");
- //Adding elements to the Linked list
- gospellist2.add("Mark");
- gospellist2.add("John");
- System.out.println("starting list");
- System.out.println("----------");
- System.out.println(gospellist2);
- System.out.println("Adding Matthew and Luke");
- System.out.println("----------");
- //Adding an element to the first position
- gospellist2.addFirst("Matthew");
- gospellist2.add(2, "Luke");
- System.out.println(gospellist2);
- //Adding an element to the last position
- gospellist2.addLast("Acts");
- //Iterating LinkedList
- System.out.println("New list of books with an iterator:");
- System.out.println("----------");
- Iterator<String> iterator=gospellist2.iterator();
- while(iterator.hasNext()){
- System.out.println(iterator.next());
- }
- //Removing Acts
- gospellist2.removeLast();
- /*This is how to get and set Values*/
- Object firstvar = gospellist2.get(0);
- System.out.println("First element: " +firstvar);
- System.out.println();
- gospellist2.set(0, "New Testiment");
- Object firstvar2 = gospellist2.get(0);
- System.out.println("First element after update by get and set method: " +firstvar2);
- System.out.println();
- System.out.println("new list of elements after update by set method: " +gospellist2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement