0% found this document useful (0 votes)
53 views

Programs - ArrayLists and LinkedList

This document contains examples of using ArrayList in Java. It shows how to create an ArrayList of custom CountryCapital objects that have country and capital string fields. Methods are demonstrated for adding CountryCapital objects to the ArrayList, searching the list by country to return the corresponding capital, and displaying the list. The examples illustrate basic usage of ArrayList such as initialization, adding/retrieving elements, iteration, and searching capabilities.

Uploaded by

RollingRage
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Programs - ArrayLists and LinkedList

This document contains examples of using ArrayList in Java. It shows how to create an ArrayList of custom CountryCapital objects that have country and capital string fields. Methods are demonstrated for adding CountryCapital objects to the ArrayList, searching the list by country to return the corresponding capital, and displaying the list. The examples illustrate basic usage of ArrayList such as initialization, adding/retrieving elements, iteration, and searching capabilities.

Uploaded by

RollingRage
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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