1. Member Join Date
Dec 2008
Posts
39
Rep Power
0

## Associative Arrays

Does anyone have any examples of an associative array? I have one that I am working on but im getting nowhere fast with it. Could anyone tell me how to go about doing this? thanks.

import java.util.*;

public class AssociativeArray {

/**
* Private instance of the array count
*/
private int count;

/**
* Private instance of the default size of the associative array
*/
private int DEFAULT;

/**
*Private instance of the integer of an empty array.
*/
final int NOT_FOUND = -1;

/**
* Private instance array of cells
* i.e. key is the string that acts as an index, data is the data (duh)
*/
private Cell[] collect;

/
public AssociativeArray(){
count = 0;
collect = (Cell[])(new Cell[DEFAULT]);
}

private class Cell{

Object key;
Object data;

private Cell(Object key, Object data) {

}

public Object getKey(){
return key;
}

public void setKey(Object _key){
key = _key;
}

public Object getData(){
return data;
}

public void setData(Object _data){
data = _data;
}

}

public void clear(){

}

public Object get(Object key){
boolean found = false;

for(int i = 0; i < count; i++){
if(collect[i].getKey().equals(key)){
found = true;
return collect[i].getData();
}
}
if(found == false){
System.out.println("You entered an invalid key");
}

return key;

}

private void expandCapacity(){
Cell[] larger = (new Cell[collect.length*2]);

for(int index=0; index < collect.length; index++){
larger[index] = collect[index];
}
collect = larger;

}

boolean contains(Object key){

int search = 0;

for(int i = 0; i < count; i++){
if(key.equals(collect[i].getKey())){
search = i;
}
}

return (search != NOT_FOUND);
}

boolean isEmpty(){
return (count == 0);
}

public void put(Object _key, Object _data){
for(int i = 0; i < count; i++){
if(size() == collect.length){
expandCapacity();
}
if(!(contains(_key))){
collect[count] = new Cell(_key, _data);
count++;
}

else if(contains(_key)){
for(int j = 0; j < count; j++){
collect[j].setData(_data);
collect[count] = new Cell(_key, _data);
count++;
}
}
}

}

void remove(Object _key){

int search = NOT_FOUND;
//If it's empty, we're going to throw a NullPointerException
if(isEmpty()){
throw new NullPointerException();
}
//Searching for the key
for(int index=0; index < count && search == NOT_FOUND; index++){
if(collect[index].getKey() == _key){
search = index;
}

}
if(search == NOT_FOUND){
System.out.println("There is no such key. Try again.");
}

//Otherwise, delete it!
else{

collect[search] = collect[count-1];
collect[count-1] = null;
count--;
}

}
//Correct
int size(){
return count;
}

//Later
public String toString(){
String info = "";
for(int i = 0; i < count; i++){
info = info + collect[i] + "\n";
}
return info;
}

The testing main method

public class Tester {

public static void main(String args[]){
AssociativeArray a = new AssociativeArray();
a.put("Jim","Rome");
a.put("Fred","Smith");
System.out.println(a);
//{[Julian,Dymacek], [Fred,Smith]}
System.out.println(a.get("Jim"));
//Rome
a.put("Jim","The Rome");
System.out.println(a.get("Jim"));
//The Rome
System.out.println(a);
//{[Jim,The Rome], [Fred,Smith]}

}

it prints out

You entered an invalid key
Jim
You entered an invalid key
Jim

} Reply With Quote

2. Moderator Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## If you want a collection that associates a value with some unique key use a Map. Sun's Tutorial has discussion and usage examples in the section The Map Interface.

Or is implementing this some sort of homework? If so - unless someone cares to do it for you - you would be best to work on just one piece of functionality at a time. If you get stuck post what you have and descriptions both of what it should do and what you observe it does do. Reply With Quote

3. Senior Member Join Date
Sep 2008
Posts
564
Rep Power
12

## it's also probably better to use generics for the key and value. Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
{"ft_lang_label":"__label__en","ft_lang_prob":0.5995442,"math_prob":0.8662495,"size":4429,"snap":"2020-34-2020-40","text_gpt3_token_len":1248,"char_repetition_ratio":0.11050847,"word_repetition_ratio":0.05531915,"special_character_ratio":0.33912846,"punctuation_ratio":0.18785796,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95669496,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-04T08:44:28Z\",\"WARC-Record-ID\":\"<urn:uuid:980419ad-cfbb-452f-8352-6fed82ce9c1e>\",\"Content-Length\":\"57093\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dcf9f4e3-9778-459f-9890-77ae8498e222>\",\"WARC-Concurrent-To\":\"<urn:uuid:8a22b899-7f60-46b5-8f88-479c75788a50>\",\"WARC-IP-Address\":\"163.172.65.71\",\"WARC-Target-URI\":\"https://www.java-forums.org/new-java/16441-associative-arrays.html\",\"WARC-Payload-Digest\":\"sha1:U4XJKIF4MK4DTDGVPXSOVATOR5WLAD43\",\"WARC-Block-Digest\":\"sha1:L6OUNNS26BJELXTFNCZE33N2AHE2R2ZH\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735867.23_warc_CC-MAIN-20200804073038-20200804103038-00309.warc.gz\"}"}