Вы находитесь на странице: 1из 8

Module 12: The Classes Vector, ArrayList

(CISS-110 Tom St.Louis)

Chapter Overview
In this chapter students will be introduced to the java Vector class. They will learn how collections of
items of the same type may be processed in a convenient way.

Chapter Objectives

Become aware of the class Vector


Become aware of the class ArrayList
Become aware of the class HashSet

class Vector

The class Vector can be used to implement a list


Unlike an array, the size of a Vector object can grow/shrink during program execution
You do not need to worry about the number of data elements in a vector

Chapter 10: Vector

04/22/13

Module 12: The Classes Vector, ArrayList


(CISS-110 Tom St.Louis)

Members of the Class Vector

Chapter 10: Vector

04/22/13

Module 12: The Classes Vector, ArrayList


(CISS-110 Tom St.Louis)

Members of the Class Vector (continued)

Chapter 10: Vector

04/22/13

Module 12: The Classes Vector, ArrayList


(CISS-110 Tom St.Louis)

Members of the Class Vector (continued)

Chapter 10: Vector

04/22/13

Module 12: The Classes Vector, ArrayList


(CISS-110 Tom St.Louis)

class Vector

Every element of a Vector object is a reference variable of the type Object


To add an element into a Vector object:
Create appropriate object
Store data into object
Store address of object holding data into Vector object element
Vector<String> stringList = new Vector<String>();
stringList.add("Spring");
stringList.add("Summer");
//add and addElement do the exact same thing!
stringList.addElement("Fall");
stringList.addElement("Winter");
stringList.remove(Summer); //remove an element from the Vector

class Vector (continued)

The class Vector is contained in the package java.util


Programs must include either:
import java.util.*;

Chapter 10: Vector

04/22/13

Module 12: The Classes Vector, ArrayList


(CISS-110 Tom St.Louis)

import java.util.Vector;

Primitive Data Types and the class Vector

Every component of a Vector object is a reference


Primitive data types are not objects
Corresponding to each primitive data type, Java provides a wrapper class
JDK 5.0 provides autoboxing and auto-unboxing of primitive data types
Creating a Vector of Integer objects
Vector<Integer> list = new Vector<Integer>();
list.add(13);
list.add(25);

Vector Objects and the foreach Loop

Each Vector object is a collection of elements


You can use a foreach loop to process its elements
Syntax:
for (type identifier : vectorObject)
statements

Chapter 10: Vector

04/22/13

Module 12: The Classes Vector, ArrayList


(CISS-110 Tom St.Louis)

class ArrayList

Every element of a ArrayList object is a reference variable of the type Object


To add an element into an ArrayList object:
Create appropriate object
Store data into object
Store address of object holding data into ArrayList object element
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Spring");
stringList.add("Summer");
stringList.add("Fall");
stringList.add("Winter");

The class ArrayList is contained in the package java.util


Programs must include either:
import java.util.*;
import java.util.ArrayList;

class HashSet

Every element of a HashSet object is a reference variable of the type Object


To add an element into an HashSet object:
Create appropriate object
Store data into object
Store address of object holding data into HashSet object element
HashSet<String> stringSet = new HashSet<String>();
stringSet.add("Spring");
stringSet.add("Summer");
stringSet.add("Fall");
stringSet.add("Winter");

The class HashSet is contained in the package java.util


Programs must include either:
import java.util.*;
import java.util.HashSet ;

Notice how ArrayList, HashSet how similar they are to use!!!

Chapter 10: Vector

04/22/13

Module 12: The Classes Vector, ArrayList


(CISS-110 Tom St.Louis)

Chapter Summary

The class Vector

Members of the class Vector

Chapter 10: Vector

04/22/13

Вам также может понравиться