Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.2 kB
0
Indexable
Never
ArrayLIst

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package arraylistcollection;
import java.util.ArrayList;

/**
 *
 * @author HP
 */
public class ArrayListCollection {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        ArrayList<String> items = new ArrayList<String>();
        items.add("red");
        items.add(0,"yellow");
        System.out.print("Display list contents with enhanced for statement");
        
        for(int i=0; i<items.size(); i++)
            System.out.printf("%s", items.get(i));
        
        display(items,"%nDisplay List contents");
        
        items.add("green");
        items.add("yellow");
        
        display(items,"list with two new elements");
        
        items.remove("yellow");
        
        display(items,"Remove first instance of yellow");
        
        items.remove(1);
        display(items,"Remove second list elements");
        
        System.out.printf("\"red\" is %s in the list", items.contains("red") ? "": "not");
        
        System.out.printf("Size %s %n", items.size());
        
    }
    
    public static void display(ArrayList<String> items, String header)
    {
        System.out.printf(header);
        
        for(String item:items)
            System.out.printf("%s",item);
        
        
            System.out.println();
    }
}

testlinkdelist

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testarrayandlinkedlist;
import java.util.*;

/**
 *
 * @author HP
 */
public class TestArrayAndLinkedList {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);        
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        arrayList.add(5);
        arrayList.add(6);
        arrayList.add(6);        
        arrayList.add(0,10);
        arrayList.add(3,30);
        
        System.out.println("A list of integers on the array list");
        System.out.println(arrayList);
        
        LinkedList<Object> linkedList = new LinkedList<>(arrayList);
        linkedList.add(1,"red");
        linkedList.removeLast();
        linkedList.addFirst("green");
        
        System.out.println("Display the linked list FORWARD");
        ListIterator<Object> listIterator = linkedList.listIterator();
        while(listIterator.hasNext()){
            System.out.println(listIterator.next() + " ");
        }
        
        System.out.println();
        System.out.println("Display the linked list Backword");
        listIterator = linkedList.listIterator(linkedList.size());
        while(listIterator.hasPrevious()){
            System.out.println(listIterator.previous() + " ");
        }
    }
    
}