Storing User-Defined Classes in Collections
import java.util.LinkedList;
class Address {
private String name;
private String street;
private String city;
private String state;
private String code;
Address(String n, String s, String c, String st, String cd) {
name = n;
street = s;
city = c;
state = st;
code = cd;
}
public String toString() {
return name + "\n" + street + "\n" + city + " " + state + " " + code;
}
}
public class Main {
public static void main(String args[]) {
LinkedList<Address> ml = new LinkedList<Address>();
ml.add(new Address("A", "11 Ave", "City", "IL", "00000"));
ml.add(new Address("B", "11 Lane", "Town", "IL","99999"));
ml.add(new Address("T", "11 St", "Province", "IL", "11111"));
for (Address element : ml){
System.out.println(element + "\n");
}
}
}
Home
Java Book
Collection
Java Book
Collection
LinkedList:
- LinkedList class
- Create LinkedList
- Add element to LinkedList
- Remove all elements from LinkedList
- Shallow copy of a LinkedList
- If contain a certain element
- Get iterator from LinkedList
- Peek the element
- Get the element from LinkedList
- Get the index of an element
- Poll, pop and push element to a LinkedList
- Remove element from a LinkedList
- Replace the element at the position
- Get the size of a LinkedList
- Convert LinkedList to Array
- Storing User-Defined Classes in Collections