Java Collection: List Interface - BunksAllowed

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

Community

demo-image

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 + "]";
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Source code of TestMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.