Untitled

 avatar
unknown
plain_text
3 years ago
3.1 kB
3
Indexable
------ Array List -----

/*
 * 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 Students
 */
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("green");
        System.out.print("Print Array List With Loop");
        for(int i = 0 ; i < items.size(); i++){
            System.out.printf(" %s ",items.get(i));
        }
        display(items, "\n Display content with enhanced for statement");
        items.add("yellow");
        items.add("purple");
        items.remove("purple");
        display(items, "\n Remove first Instance");
        items.remove(1);
        display(items, "\n Remove second Instance");
        System.out.printf("\"red\" is %s in the list %n",items.contains("red") ?"" : "not"  );
        System.out.printf("%s", 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();
        }
        
    }
    
}
----- Test Queue-------
/*
 * 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 testqueue;

/**
 *
 * @author Students
 */
public class TestQueue {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        java.util.Queue <String> items = new java.util.LinkedList<>();
        items.offer("bd");
        items.offer("india");
        items.offer("pakistan");
        items.offer("usa");
        while(items.size() > 0){
            System.out.print(items.remove() + "");
        }
        
    }
    
}
----- Iterator -----
/*
 * 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 testiterator;
 import java.util.*;
/**
 *
 * @author Students
 */
public class TestIterator {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Collection<String> collection = new ArrayList<>();
        collection.add("New York");
        collection.add("Atlanta");
        collection.add("Dallas");
        collection.add("Madison");
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next().toUpperCase() + " ");
        }
        System.out.println();
    }
    
    
}


Editor is loading...