Collection

Disadvantage of using array:

java.util

We will mainly talk about 2 classes There are some other useful classes :

Vector and LinkedList

Vector

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

LinkedList

Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).

Common API for Vector and LinkedList

Methods from the interface List

Methods Description
void add(int index, Object element) Inserts the specified element at the specified position in this list.
boolean add(Object o) Appends the specified element to the end of this list
void clear() Removes all of the elements from this list.
boolean contains(Object o) Returns true if this list contains the specified element.
Object get(int index) Returns the element at the specified position in this list.
int indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
boolean isEmpty() Returns true if this list contains no elements.
int lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
Object remove(int index) Removes the element at the specified position in this list.
boolean remove(Object o) Removes the first occurrence in this list of the specified element .
Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element.
int size() Returns the number of elements in this list.
Object[] toArray() Returns an array containing all of the elements in this list in proper sequence.

More methods for Vector classes

Constructor/Methods Description
Vector(),Vector(int initialCapacity) Constructs an empty vector with the specified initial capacity, the default is 10.
Object firstElement() Returns the first component (the item at index 0) of this vector.
Object lastElement() Returns the last component of the vector.

More methods for LinkedList classes

Methods Description
LinkedList() Constructs an empty list.
void addFirst(Object o) Inserts the given element at the beginning of this list.
void addLast(Object o) Appends the given element to the end of this list.
Object getFirst() Returns the first element in this list.
Object getLast() Returns the last element in this list.
Object removeFirst() Removes and returns the first element from this list.
Object removeLast() Removes and returns the last element from this list.

Example

Book.java

Remember your first HW. You can get the program here.
   1:public class Book {
   2:    private String title;
   3:    private String author;
   4:    public Book(String myAuthor, String myTitle) {
   5:        title = myTitle;
   6:        author = myAuthor;
   7:    }
   8:    
   9:    boolean doesTitleContain(String word) {
  10:        String lowerCaseTitle = title.toLowerCase();
  11:        String lowerCaseWord = word.toLowerCase();
  12:        return lowerCaseTitle.indexOf(lowerCaseWord) >= 0;
  13:    }
  14:    
  15:    boolean doesAuthorContain(String word) {
  16:        String lowerCaseAuthor = author.toLowerCase();
  17:        String lowerCaseWord = word.toLowerCase();
  18:        return lowerCaseAuthor.indexOf(lowerCaseWord) >= 0;
  19:    }
  20:    
  21:    public String showInfo() {
  22:        return "Title: " + title + "\n" +
  23:        "Author: " + author + "\n";
  24:    }
  25:}
  26:

Library.java

You can get the program here.
Screen shot
lib1.gif

After you click the "add new item" button.
lib2.gif

After you click the "show all" button
lib3.gif

Search for the keyword "Charles", press return at the textfield
lib4.gif

   1:import java.awt.*;
   2:import javax.swing.*;
   3:import java.awt.event.*;
   4:import java.util.*;
   5:
   6:public class Library extends JFrame {
   7:    JPanel northPanel = new JPanel(new GridLayout(2,2));
   8:    JTextArea display = new JTextArea(8,40);
   9:    Vector bookList = new Vector();
  10:    JButton addButton = new JButton("Add New Item");
  11:    JButton allButton = new JButton("Show All Books");
  12:    JLabel searchLabel = new JLabel("Enter Keyword :");
  13:    JTextField searchTextField= new JTextField();
  14:    
  15:    // The frame for input books
  16:    JFrame inputBookFrame = new JFrame();
  17:    JLabel inputAuthorLabel = new JLabel("Name :");
  18:    JLabel inputTitleLabel = new JLabel("Title :");
  19:    JTextField inputAuthorTextField = new JTextField(30);
  20:    JTextField inputTitleTextField = new JTextField(30);
  21:    JButton inputAddButton = new JButton("Add");
  22:    
  23:    
  24:    public Library() {
  25:        display.setLineWrap(true);
  26:        display.setWrapStyleWord(true);
  27:        display.setEditable(false);
  28:        
  29:        northPanel.add(searchLabel);
  30:        northPanel.add(searchTextField);
  31:        northPanel.add(addButton);
  32:        northPanel.add(allButton);
  33:        getContentPane().add(northPanel, BorderLayout.NORTH);
  34:        getContentPane().add(new JScrollPane(display));
  35:        
  36:
  37:        
  38:        setUpInputBookFrame();
  39:        
  40:        addButton.addActionListener(new AddButtonListener());
  41:        allButton.addActionListener(new ShowAllButtonListener());
  42:        searchTextField.addActionListener(new SearchListener());
  43:    }
  44:    
  45:    public void setUpInputBookFrame() {
  46:        JPanel tempWestPanel = new JPanel(new GridLayout(0,1));
  47:        JPanel tempCenterPanel = new JPanel(new GridLayout(0,1));
  48:        tempWestPanel.add(inputAuthorLabel);
  49:        tempWestPanel.add(inputTitleLabel);
  50:        tempCenterPanel.add(inputAuthorTextField);
  51:        tempCenterPanel.add(inputTitleTextField);
  52:        inputBookFrame.getContentPane().add(tempCenterPanel);
  53:        inputBookFrame.getContentPane().add(tempWestPanel, BorderLayout.WEST);
  54:        inputBookFrame.getContentPane().add(inputAddButton, BorderLayout.SOUTH);
  55:        
  56:        inputAddButton.addActionListener(new InputAddButtonListener());
  57:    }
  58:    
  59:    // add the book to the date base
  60:    public void addBook(Book book) {
  61:        // add to the bookList
  62:        bookList.add(book);
  63:    }
  64:    
  65:    public void showAll() {
  66:        display.setText("");
  67:        Book tempBook = null;
  68:        for(int i = 0; i < bookList.size(); i++) {
  69:            display.append((i+1) + ".\n");
  70:            // the follow line casts Object to Book
  71:            tempBook = (Book)bookList.get(i); 
  72:            display.append(tempBook.showInfo());
  73:        }
  74:    }
  75:    
  76:    
  77:    class AddButtonListener implements ActionListener {
  78:        public void actionPerformed(ActionEvent e) {
  79:            inputBookFrame.pack();
  80:            inputBookFrame.setVisible(true);
  81:        }
  82:    }
  83:    
  84:    class InputAddButtonListener implements ActionListener {
  85:        public void actionPerformed(ActionEvent e) {
  86:            addBook(new Book(inputAuthorTextField.getText(),
  87:                             inputTitleTextField.getText()));
  88:            inputBookFrame.setVisible(false);
  89:            inputAuthorTextField.setText("");
  90:            inputTitleTextField.setText("");
  91:        }
  92:    }
  93:    
  94:    class ShowAllButtonListener implements ActionListener {
  95:        public void actionPerformed(ActionEvent e) {
  96:            showAll();
  97:        }
  98:    }
  99:    
 100:    class SearchListener implements ActionListener {
 101:        public void actionPerformed(ActionEvent e) {
 102:            String keyword = searchTextField.getText();
 103:            display.setText("");
 104:            int counter = 0;
 105:            Book tempBook;
 106:            for(int i = 0; i < bookList.size(); i++ ){
 107:                tempBook = (Book)bookList.get(i);
 108:                if(tempBook.doesTitleContain(keyword) || 
 109:                  tempBook.doesAuthorContain(keyword)) {
 110:                    counter++;
 111:                    display.append(counter + ".\n");
 112:                    display.append(tempBook.showInfo());
 113:                  }
 114:            }
 115:            
 116:            if(counter == 0) {
 117:                display.append("No book matches the keywork is found\n");
 118:            }
 119:        }
 120:    }
 121:    
 122:    public static void main(String[] args) {
 123:        Library lib = new Library();
 124:        lib.pack();
 125:        lib.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 126:        lib.setVisible(true);
 127:    }
 128:}  
Highlight

Question

How to create a vector consists of integers?