Results 1 to 3 of 3
Thread: Associative Arrays
- 02-28-2009, 05:10 AM #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;
}
}
//It's not found, screw it!
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
}
- 02-28-2009, 06:00 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 02-28-2009, 09:32 AM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Similar Threads
-
Help!! With arrays
By ookie833 in forum New To JavaReplies: 8Last Post: 12-14-2008, 12:57 AM -
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM -
Help on Arrays...
By cuellar14 in forum New To JavaReplies: 4Last Post: 07-25-2008, 08:16 PM -
new to arrays
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-08-2008, 02:45 PM -
2D-Arrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02-07-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks