Vectors - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Vectors

Description:

A Vector is another data structure that has some advantages and ... Erasing data. v.remove(int index); // Removes element at index // Lower elements shuffle up ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 19
Provided by: philli89
Category:

less

Transcript and Presenter's Notes

Title: Vectors


1
Vectors
  • An Array has some limitationsFixed size,
    Homogeneous element types(This is not the case
    with an array of Objects)
  • A Vector is another data structure that has some
    advantages and some useful methods.

2
Vectors (2)
  • A Vector only stores Objects.(Which can
    represent any other object type)
  • A vector has dynamic size allocation. It expands
    its capacity as required.
  • Dynamically adjusts when elements are inserted
    or removed.

3
Vector methods
  • ConstructorVector v new Vector()Vector v
    new Vector(int size)Vector v new Vector(int
    size, int incr)
  • Adding elementsv.addElement(Object x) // Adds
    element at endv.insertElementAt(Object x,int
    index) //adds at index shuffles other
    elements backv.setElementAt(Object o, int indx)
    // overwrites existing element
    Retrieving Datav.elementAt(in
    t index) // gets element at index
  • Erasing datav.remove(int index) // Removes
    element at index // Lower
    elements shuffle upv.clear() //
    Clears all elements

4
Vectors use polymorphism
  • Vectors only store (Capital O) Objectsthe
    ancestor class of all Java classes.
  • Since a superclass can represent any class below
    it in the inheritance hierarchy, an Object can
    represent an object of any Javaclass. Storing
    Objects. Vector v new Vector
    v.addElement(Test) v.addElement(new
    Date()) v.addElement(new Coordinate(2.3,4.5)

5
Objects in Vectors
  • The objects are automatically cast to Objects.
    (Implicit casting).
  • A Vector only returns Objects. Before objects can
    be used they must be cast back to their original
    type. (explicit cast). String s1
    (String)v.Element(0)?String s1
    (String)v.Element(1)?But an explicit cast must
    match previous implicit cast, else RunTime error!

6
Reading from text files
  • This section is a short introduction to file
    handling. More later.
  • File I/O is handled by data stream objects
  • Reading from a text file ( a file of ASCII
    characters) uses a FileReader objectFileReader
    fr new FileReader(Filename)

7
Text files(2)
  • A BufferedReader object provides methods to read
    the data from a FileReader object.BufferedReader
    br new BufferedReader(fr)
  • Use the readLine() method to read each line of
    the file as a String.String str
    br.readLine()Each line can be broken into
    smaller strings using the StringTokenizer.

8
Text Files (3)
  • Consider a file data.txt that contains the
    following lines. John Smith LMIF Sue
    Jones LBCF James Wilson LMIF Mary
    Collins LMIFTo open and read the data from the
    fileFileReader fr new FileReader(data.txt)
    BufferedReader br new BufferedReader(fr)String
    s br.readLine()while(s!null)
    System.out.println(s) s br.readLine()

9
Text Files (4)
  • Data Streams must have exception handling (Error
    checking) in place. Requires Try and Catch
    blocks.try FileReader fr new
    FileReader(data.txt) BufferedReader br new
    BufferedReader(fr) String str br.readLine()
    while(str!null) System.out.println(str)
    strbr.readLine() br.close()catc
    h(IOException e) System.out.println(I/O
    error)

10
Writing to a text file
  • Use FileWriter object to open the file.
    FileWriter fw new FileWriter(filename)
  • The PrintWriter object provides methods to write
    Strings to the file. PrintWriter pw new
    PrintWriter(fw)
  • Write to file using the println() method.
    pw.println(This goes to the file)
  • Uses the same format as System.out.println()

11
Writing to a file (2)
  • try
  • FileWriter fw new FileWriter(out.txt)Print
    Writer pw new PrintWriter(fw)pw.println(This
    is the first line)pw.println(This is the next
    line)pw.println(This is the last line)
  • pw.close()
  • catch (IOException e)
  • The data is not written to the file until the
    PrintWriter object is closed or is flushed.

12
Object Serialization
  • Designed as a way of storing Graphics objects so
    components hidden by a window can be restored
    when the window closes.Called Serialization
    because it allows us to pick up where we left off
    after some interruption is finished. Like a TV
    serial.
  • Serializable objects can be stored and retrieved
    from a file as single entities.An array is an
    object. Object Serialization allows us to store
    the array and all its contents in one step.

13
Object Serialization (2)
  • Consider an array of Student objects. The
    Students contain contain Course objects as data
    members and the Course objects contain other
    objects as their data members.

Course 1
Course 2
Student 1
Course 3
To file
Student 2
Array
Course 8
Student 3
Course 3
Course 5
Course 2
Course 7
Serialization lets us handle the array as a
single object.All its internal structure is
carried with it without needingspecial treatment.
14
Object Serialization (3)
  • The classes that define the objects we wish to
    save using OS must implement the Serializable
    interface. This is a marker interface that
    enables the process.
  • OS works through ObjectDataStream
    objects.ObjectInputStream and ObjectOutPutStream.
    These connect to the file through DataStream
    objects DataInputStream and DataOutputStream.
  • Object Serialization only stores and retrieves
    Objects, like a Vector, so Polymorphism, implicit
    and explicit casting are involved.

15
Object Serialization (4)
  • When loading an object using OS then the class of
    the Object and the classes of all other objects
    that may be contained within the Object must be
    known on the system to which the the Object is
    loaded.
  • This must be checked by including
    aClassNotFoundException catchin addition to the
    IOException catch.

16
Object Serialization Example
  • This example stores an array of Coordinate
    objects.The Coordinate class must implement
    Serializable.
  • import java.io.
  • public class Coordinate implements
    Serializable double x, y public
    Coordinate(double x, double y)
  • this.xx this.yy public String
    toString() return Coord x x ,
    y

17
OS Example (2). Saving Objects
  • import java.io.
  • public class SaveCoord
  • Coordinate ca new Coordinate10public
    static void main (String args)
  • SaveCoord sc new SaveCoord() sc.ca0new
    Coordinate(2.3,4.5)
  • sc.ca1new Coordinate(3.1,6.5)
  • sc.ca2new Coordinate(4.3,1.5)
  • try FileOutputStream d new
    FileOutputStream(d.dat)ObjectOutputStream o
    new ObjectOutputStream(d)o.writeObject(sc.ca)
  • o.close()
  • catch (IOException x)

18
OS Example (3). Loading Objects
  • import java.io.
  • public class LoadCoord
  • Coordinate ca
  • public static void main(String args)
  • LoadCoord lc new LoadCoord()try
    FileInputStream d new FileInputStream(d.dat)
    ObjectInputStream in new ObjectInputStream(d)
    lc.ca (Coordinate ) in.readObject()// Cast
  • in.close() catch (IOException e)
  • catch (ClassNotFoundException cnf)
  • //All classes used by the object must be
    knownSystem.out.println(lc.ca0)
  • System.out.println(lc.ca1)System.out.println
    (lc.ca2)
Write a Comment
User Comments (0)
About PowerShow.com