[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Multidimensional hashmap or something alse array related.

Posted on 2005-05-16
3
Medium Priority
?
2,851 Views
Last Modified: 2008-01-09
I need something in Java that would allow me to set multidimentional arrays with the ability to set String values as both as keys  and as the value.  Also, I need to be able to check if the keys exists within one and two dimentions.  Something like.

myPlan[0][0] = "cell 1"
myPlan[0][1] = "cell 2"
myPlan[0][2] = "cell 3"
myPlan[1][0] = "cell 4"
myPlan[1][1] = "cell 5"
myPlan[1][2] = "cell 6"

than I need to be able to check if centerain keys exist for the first set/dimention, for example myPlan.containsKey(0), as well as for both dimentions myPlan.containsKey(0)(2).  I don't want to check for values it has to check if the keys exist, which at times may be Strings.

I'm not sure if HashMap is even the correct thing to use in this instance and I'm not familiar with Vectors to start figuring it out on my own.  

Thanks for your help and recommenations.
Kasia
0
Comment
Question by:kasiencja
3 Comments
 

Author Comment

by:kasiencja
ID: 14013786
Hmm, I just looked over the code again and it looks like I might be able to get away with the key values being set to int only.
0
 
LVL 13

Accepted Solution

by:
petmagdy earned 1600 total points
ID: 14014038
Hi kasiencja,

u can still use a single Hastable object, but the key will be a Class u will define (Key2D) that implements equals() and hashCode() Methods, this will be the Key and the Value will  be the string:

public class Key2D {
 int xCordinate;
 int yCordinate;

 public boolean equals(object) {
   if(object instanceof Key2D) {
    // cast object to Key2D and compare x and y
   }
 }

 public int hashCode() {
    //Convert x, y to Strings and concatinate them with space badding based on maximum lenght and then return the hashCode of the result String
   return concatentaionResult.hashCode();
 }
}


now u can search the Hashtable using hashtable variable.containsKey(instanceOfKey2D)

u can also use any Map based object instead of Hashtable like HashMap or TreeMap, etc.....
Cheers!
0
 
LVL 92

Expert Comment

by:objects
ID: 14014587
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question