Programs - ArrayLists and LinkedList

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CS F213- Object Oriented Programming

ArrayList (Examples)

1 $vi CountryCapitalWithArrays.java
import java.util.ArrayList;

class CountryCapital {
private String country;
private String capital;

public CountryCapital (String c1, String c2) {


country = c1; capital = c2;
}

public String getCountry() { return country; }


public String getCapital() { return capital; }
public void display() {
System.out.println(country + " : " + capital);
}
} // of CountryCapital

public class CountryCapitalWithArrayTester {

public static void main(String[] args) {


CountryCapital[] ccList = new CountryCapital[10];
int count = 0;
CountryCapital cc = new CountryCapital(“UAE”, “Abu Dhabi”);
ccList[count++] = cc;
CountryCapital cc = new CountryCapital("INDIA", "Delhi");
ccList[count++] = cc;

Boolean found = false;


String capital = "";
String country = “INDIA”;
for (CountryCapital cc : ccList) {
if (cc.getCountry().equals(country)) {
found = true
SOP( cc.getCapital();
break;
} //of if
} // of for
if (!found) { SOP (“not found”); }
}
}
1 $vi CountryCapitalArrayListTester.java
import java.util.ArrayList;

class CountryCapital {
private String country;
private String capital;

public CountryCapital (String c1, String c2) {


country = c1; capital = c2;
}

public String getCountry() { return country; }


public String getCapital() { return capital; }
public void display() {
System.out.println(country + " : " + capital);
}
} // of CountryCapital

class CountryCapitalList {

private ArrayList<CountryCapital> ccList;

public CountryCapitalList() {
ccList = new ArrayList<CountryCapital>();
}
public void add (String c1, String c2) {
CountryCapital cc = new CountryCapital(c1, c2);
ccList.add(cc);
}

public void add (String c1, String c2) {


CountryCapital cc = new CountryCapital(c1, c2);
ccList[count++] = cc;
}
public String searchCCapital (String country) {
String capital = "";
for (CountryCapital cc : ccList) {
if (cc.getCountry().equals(country) ) return cc.getCapital();
}
return capital;
}

public void display() {


for (CountryCapital cc : ccList) {
cc.display();
}
}
} /of CountryCapitalList
public class CountryCapitalArrayListTester {

public static void main(String[] args) {


CountryCapitalList alist = new CountryCapitalList();
alist.add("UAE", "Abu Dhabi");
alist.add("INDIA", "Delhi");
alist.display();

String capital = alist.searchCCapital("UAE");


// if capital.equals("") (i.e. the country doesnot exists in the
list
System.out.println(capital);
}
}

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);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy