Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in java, where would the line if (N >= vals.length) resize (2*N); go in the following code and why? public class ArrayST { private static

in java, where would the line if (N >= vals.length) resize(2*N); go in the following code and why?

public class ArrayST {

private static final int INIT_SIZE = 8;

private Value[] vals; // symbol table values

private Key[] keys; // symbol table keys

private int N = 0; // number of elements in symbol table

public ArrayST() {

keys = (Key[]) new Object[INIT_SIZE];

vals = (Value[]) new Object[INIT_SIZE];

}

// return the number of key-value pairs in the symbol table

public int size() { return N; }

// is the symbol table empty?

public boolean isEmpty() { return size() == 0; }

// resize the parallel arrays to the given capacity

private void resize(int capacity) {

Key[] tempk = (Key[]) new Object[capacity];

Value[] tempv = (Value[]) new Object[capacity];

for (int i = 0; i < N; i++) tempk[i] = keys[i];

for (int i = 0; i < N; i++) tempv[i] = vals[i];

keys = tempk;

vals = tempv;

}

// insert the key-value pair into the symbol table

public void put(Key key, Value val) {

// to deal with duplicates

delete(key);

// add new key and value at the end of array

vals[N] = val;

keys[N] = key;

N++;

}

public Value get(Key key) {

for (int i = 0; i < N; i++)

if (keys[i].equals(key)) return vals[i];

return null;

}

public Iterable keys() {

Queue queue = new Queue();

for (int i = 0; i < N; i++)

queue.enqueue(keys[i]);

return queue;

}

// remove given key (and associated value)

public void delete(Key key) {

for (int i = 0; i < N; i++) {

if (key.equals(keys[i])) {

keys[i] = keys[N-1];

vals[i] = vals[N-1];

keys[N-1] = null;

vals[N-1] = null;

N--;

return;

}

}

}

/***********************************************************************

* Test routine.

**********************************************************************/

public static void main(String[] args) {

ArrayST st = new ArrayST();

for (int i = 0; !StdIn.isEmpty(); i++) {

String key = StdIn.readString();

st.put(key, i);

}

for (String s : st.keys())

StdOut.println(s + " " + st.get(s));

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions

Question

LO2 Discuss important legal areas regarding safety and health.

Answered: 1 week ago