Java Collection: List Interface - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Java Collection: List Interface

Share This

The List interface is the sub-interface of Collection interface. The following program shows how this interface is used.

Source code of Item.java
package com.t4b.test; public class Item { String name; int id; double price; public Item(String name, int id, double price) { super(); this.name = name; this.id = id; this.price = price; } @Override public String toString() { return "Item [name=" + name + ", id=" + id + ", price=" + price + "]"; } }

Source code of TestMain.java
package com.t4b.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class TestMain { public static void main(String[] args) { List<Item> items = new ArrayList<Item>(); items.add(new Item("Apple", 1, 150.0)); items.add(new Item("Bag", 3, 1050.0)); items.add(new Item("Grape", 2, 250.0)); Iterator<Item> itr = items.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } items.remove(1); itr = items.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } items.add(1, new Item("Apple", 1, 150.0)); for (Item item : items) { System.out.println(item); } Item item = items.get(1); System.out.println(item); items.set(1, new Item("Apple", 1, 200.0)); for (Item i : items) { System.out.println(i); } } }

Happy Exploring!

No comments:

Post a Comment