Programs - ArrayLists and LinkedList
Programs - ArrayLists and LinkedList
Programs - ArrayLists and LinkedList
ArrayList (Examples)
1 $vi CountryCapitalWithArrays.java
import java.util.ArrayList;
class CountryCapital {
private String country;
private String capital;
class CountryCapital {
private String country;
private String capital;
class CountryCapitalList {
public CountryCapitalList() {
ccList = new ArrayList<CountryCapital>();
}
public void add (String c1, String c2) {
CountryCapital cc = new CountryCapital(c1, c2);
ccList.add(cc);
}
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class LinkedListExample {
public static void main(String[] args) {
//Creating list of Books
List<Book> list=new LinkedList<Book>();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc
Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to list
list.add(b1);
list.add(b2);
list.add(b3);
//Traversing list
for(Book b:list){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}