Untitled
unknown
java
3 years ago
2.7 kB
6
Indexable
/* * 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 lab10; import java.util.ArrayList; /** * * @author B2-517 */ public class ArrayListCollection { public static void display() { ArrayList<String> items = new ArrayList<String>(); items.add("Red"); items.add(0,"Yellow"); System.out.println("Display list connects with counter-controlled loop:"); for(int i = 0; i < items.size(); i++) { System.out.printf("%s \n",items.get(i)); } iterate(items, "\n Display list contents with enhanced for statement"); items.add("Green"); items.add("Yellow"); iterate(items, "\n Display with two new items"); items.remove("Green"); iterate(items, "\n Remove Second Element (Green)"); } public static void iterate(ArrayList<String> items, String header) { System.out.println(header); for(String item : items) { System.out.printf("%s \n",item); System.out.println(); } } } /* * 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 lab10; import java.util.Queue; import java.util.LinkedList; /** * * @author B2-517 */ public class TestQueue { public static void test() { Queue<String> queue = new LinkedList(); queue.offer("Saint Martin"); queue.offer("Sunamgonj"); queue.offer("Sikkim"); queue.offer("Ladakh"); queue.offer("Kashmir"); queue.offer("Berlin"); while(queue.size() > 0) { System.out.print(queue.remove() + "\n"); } } } /* * 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 lab10; /** * * @author B2-517 */ public class Lab10 { /** * @param args the command line arguments */ public static void main(String[] args) { ArrayListCollection collection = new ArrayListCollection(); collection.display(); System.out.println("-------------- Queue ----------------"); TestQueue queue = new TestQueue(); queue.test(); } }
Editor is loading...