The Artima Developer Community
Sponsored Link

Java Answers Forum
mapping a int

4 replies on 1 page. Most recent reply: Feb 4, 2003 12:16 PM by ben

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

mapping a int Posted: Feb 4, 2003 4:07 AM
Reply to this message Reply
Advertisement
hello
I have this code below which maps out my string keys with string values; once it has maped them out it gets the string keys in the order that they are maped out at.
my problem is that i want to map some integers out with them ( int keys[]={0,1,2,3,4,5,6,7,8,9} )
How would i do this?
please help me
ben

Hashtable ht2 = new Hashtable();
//Add vector to store results
Vector results2 = new Vector();
Object value2 = null;
String keys2[] = {"FirstName", "LastName", "Id", "Addr1", "Phone","County", "Postcode", "Country", "Addr2", "Town"};
//System.out.println(keys2);
String values2[] = {"ben", "ben", "ben", "mum", "ben","mum", "ben", "ben", "mum", "mum"};
// First, map keys to values in a hashtable
Hashtable hash2 = new Hashtable();
// the code matches values one by one for each of the keys
for(int i = 0; i < keys2.length; i++) {
hash2.put(keys2[i], values2[i]);
}
// Then we find each value and remove it from the hashtable
for(int j = 0; j < values2.length; j++) {
ht2 = (hash2);
value2 = (values2[j]);
Vector v = new Vector();
if( ht2.containsValue( value2 )) {
Enumeration e = ht2.keys();
while (e.hasMoreElements()) {
String tempkey2 = (String)e.nextElement();
String tempvalue2 = (String)ht2.get(tempkey2);
if (tempvalue2.equals(value2)) {
v.add(tempkey2);
ht2.remove(tempkey2);
}
}
results2.add(v);
}

}


Mike Penner

Posts: 16
Nickname: mikepenner
Registered: Jan, 2003

Re: mapping a int Posted: Feb 4, 2003 7:32 AM
Reply to this message Reply
It looks like you want to use an int as the index into a Hashtable. This is not immediately possible, since indexes into Hashtables (and Maps, etc.) must be Objects and ints are primitives, not Objects.

The way to get around this is to wrap your int in an Object. The java.lang.Integer class extends Object. It also has a constructor that accepts an int (new Integer(someint) and a method that returns its int value (intValue).

So, if myint is an int, then hash2.put(myint,"three") won't compile, but hash2.put(new Integer(myint),"three") will compile and run.

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: mapping a int Posted: Feb 4, 2003 9:30 AM
Reply to this message Reply
I know i can use this idea you have told me.
But my problem is that i need to add another line so i have three lines like this

int nums[] = {0,1,2,3,4,5,6,7,8,9};

String keys2[] ={"FirstName", "LastName", "Id", "Addr1", "Phone","County", "Postcode", "Country", "Addr2", "Town"};

String values2[] = {"ben", "ben", "ben", "mum", "ben","mum", "ben", "ben", "mum", "mum"};
and then i want to map them out so that when i find the keys that are equal to values i also want to get the nums.
for example if i get out the values that are equal to ben the out print would be this
FirstName, LastName, Id, phone, postcode, contry
but i also want to get the ints (nums) that are equal to the values so that the out print would be
0,1,2,4,6,7
how can i get my code so it will do this?
thanks
ben

Mike Penner

Posts: 16
Nickname: mikepenner
Registered: Jan, 2003

Re: mapping a int Posted: Feb 4, 2003 12:09 PM
Reply to this message Reply
If you know the index into values2 or keys2 that corresponds to a match, use that same index into nums to get the number.

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: mapping a int Posted: Feb 4, 2003 12:16 PM
Reply to this message Reply
could you show me what you mean please as i do not understand what you mean
thankyou
ben

Flat View: This topic has 4 replies on 1 page
Topic: source code of internet monitoring tool(in java) Previous Topic   Next Topic Topic: Any tools to analyze JVM Memory.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use