0% found this document useful (0 votes)
9 views17 pages

Lect 07

Uploaded by

Arnold
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views17 pages

Lect 07

Uploaded by

Arnold
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Thought for the Day

No one travels so high as he who knows not


where he is going.
– Oliver Cromwell
Section 2
Advanced Data Structures
Vectors and Lists
Stacks and Queues
Trees and Graphs
Dictionaries and Hash Tables
Chapter 4: Vectors and Lists
• Objectives
– To consider simple lists of information

– To study the implementation of simple lists:


• using arrays
• using linked lists

– To see how polymorphism and generics can be


used with ADTs in Java
Lists
• Many problems involve the use of lists
– Lists of courses
– Lists of students
– Lists of marks
– Lists of guests
– Lists of groceries A list of lists!
Lists
• In this chapter we consider:

– A list of integers, using an array

– A list of integers, using a linked list


Values
– A generic list, using polymorphism

– A generic list, using Java generics


Operations on Lists
• Basic set of operations:
– Creating an empty list
– Adding an element
– Accessing an element
– Removing an element
– Finding an element
– Finding the number of elements
– Displaying all the elements
The IntegerVector Class
• Vector
– Technical/mathematical term for a list

• Class diagram (incomplete):


IntegerVector
data, numElements
add, get, set, position,
remove, length
The IntegerVector Class
public class IntegerVector
{ private int[] data;
private int numElements;
...
} // class IntegerVector

• Data members are all private


– A very common pattern for ADTs
– Data is accessed (and protected) by public
methods
IntegerVector: Constructors
public IntegerVector (int initSize)
{ if (initSize <= 0)
throw new IllegalArgumentException(…);
numElements = 0;
data = new int[initSize];
} // constructor

public IntegerVector ()
{ this(100);
} // constructor
Default size

Call overloaded constructor


Client View: Creating Vectors
IntegerVector v1 = new IntegerVector();
IntegerVector v2 = new IntegerVector(20);
Adding Items to a Vector
public void add (int item)
// Place the new item at the end of an
// IntegerVector
{ if (numElements+1 > data.length)
throw new NoSpaceAvailableException(…);

data[numElements++] = item;
} // add

Equivalent to:
data[numElements] = item;
numElements = numElements + 1;
Adding Items (cont.)
• Adding items at an arbitrary position
public void add (int item, int pos)
{ ...
if (pos >= numElements) // Add at end
data[numElements++] = item; // As before
else
{ int k;
for (k = numElements-1; k >= pos; k--)
data[k+1] = data[k]; // Move elem’s
data[pos] = item; // Put item in place
numElements++;
}
} // add
Client View: Adding Elements
v1.add(3);
v1.add(-8);
v1.add(22, 1);
...
v2.add(39);
...

v1 3 22 -8

v2 39
Displaying Vectors
• We could write an “output” method

public void output ()


{ for (int k = 0; k < numElements; k++)
System.out.print(data[k] + " ");
System.out.println();
} // output

But this limits clients to using System.out


A Better Solution
• Override the toString method
public String toString ()
{ StringBuffer s = new StringBuffer("[");
int k;
for (k = 0; k < numElements; k++)
{ s.append(data[k]);
if (k < numElements-1)
s.append(", ");
}
s.append("]");
return s.toString();
} // toString
Client View: Displaying Vectors

System.out.println(”My vector = ” + v1);


...
System.out.println(v2);
...
textBox.setText(”v1 = ” + v1);

My vector = [3, 22, -8]


[39]

You might also like