Sarah P.

asked • 10/03/20

Solve the question

Problem: Johnny realized that there are many developers out there who will be pleased if there is a data structure that can store paired-data of int and String (this is what they call key-value paired data).


Johnny is thinking of creating an abstract data type, called "ArrayMap", which provides the set of methods as shown below (same style of diagram as used in the class).


Give an implementation that will support these APIs, using two arrays.


Array Map:

void put(int key, String value);

String get(int key);

int[] getAllKeys();

String remove(int key);

boolean isEmpty();

int size();

void clear();


Example operation:

ArrayMap map = new ArrayMap(100); // It will hold up to 100 paired data.


map.put(107, "John");

map.put(102, "Sally"); // These data will be stored as paired.

map.put(211, "Dave");

map.put(195, "Tom");


String name = map.get(211); // will return "Dave".

int[] keys = map.getAllKeys(); // will return {107, 102, 211, 195}

System.out.println(map.remove(102)); // will remove [102, Sally] pair.

System.out.println(map.isEmpty()); // will return 'false;

System.out.println(map.size()); // will return 3.

map.clear(); // will make both arrays empty.



Patrick B.

source code uploaded to RESOURCES under this link.
Report

10/03/20

1 Expert Answer

By:

Patrick B. answered • 10/03/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.