Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

How to code something in Java

Thread view: 
TheTravellingSalesman - 28 Nov 2007 20:36 GMT
I'm still relatively new to Java. I have a data set in which each pair
has a number attached to it.

Say the data set is {a,b,c,d,....z}

Now form this data set, I have bunch of pairs and each pair has a
number attached to it.
e.g. {a,b} = 3
      {a,y} = 5

I was wondering how will I code somethign like this in Java

Ideally I would have an array where I would have an input like Array[x]
[y].getValue()

Any ideas.

Thanks.
lord.zoltar@gmail.com - 28 Nov 2007 20:56 GMT
On Nov 28, 3:36 pm, TheTravellingSalesman <saad.za...@gmail.com>
wrote:
> I'm still relatively new to Java. I have a data set in which each pair
> has a number attached to it.
[quoted text clipped - 14 lines]
>
> Thanks.

Java does have multi-dimensional arrays, but you cannot index them by
character literals (if that is what you were trying to do).
someArray[x][y]=3
WILL work in Java (assuming that x and y are integer variables, but
someArray['x']['y'] =3
won't. Have you looked into HashMaps? I suppose you could have a
HashMap of HashMaps, just like you can have an array of arrays. Hard
to say more without more details on the problem.
Joshua Cranmer - 28 Nov 2007 22:09 GMT
> Java does have multi-dimensional arrays, but you cannot index them by
> character literals (if that is what you were trying to do).

Character literals work but String literals do not.

A char -> int conversion is a widening primitive conversion, and as such
does not need a cast to be converted for you. Using characters as their
integral values is actually somewhat common in many circumstances.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

rossum - 28 Nov 2007 21:13 GMT
>I'm still relatively new to Java. I have a data set in which each pair
>has a number attached to it.
[quoted text clipped - 14 lines]
>
>Thanks.
One idea would be to use a HashMap.  Use a two character string, "ab",
"ay" etc. as the key and use your number as the value.

 HashMap<String, int>

rossum
Curt Welch - 28 Nov 2007 21:52 GMT
> I'm still relatively new to Java. I have a data set in which each pair
> has a number attached to it.
[quoted text clipped - 14 lines]
>
> Thanks.

There are many options.

Is your a to z the actual data you want to use, or just an example?  Are
there exactly 26 items?

You could simply use a two dimensional int array which is 26x26 for one
simple example:

 int[][] values = new int[26][26];

 values[0][1] = 3;   // a is 0, b is 1
 values[0][24] = 5;  // a is 0, y is 24

 int v = values[0][1];

Using a 2D array means you can't tell if a given value is defined or not
because it makes it work as if all possible values are defined.  That may
not be what you need.

For an associative lookup, you will use one of the Map Collections. Java
doesn't implement a 2D associative lookup so you have to do that yourself
one way or another.  A simple way would be to use a Map with a String as a
key and the Integer as the value, and construct your keys as pairs of
letters:

  Map<String,Integer> values = new HashMap<String,Integer>();

  values.put("ab", 3);
  values.put("ay", 5);

  int v = values.get("ab");

Signature

Curt Welch                                            http://CurtWelch.Com/
curt@kcwc.com                                        http://NewsReader.Com/

lord.zoltar@gmail.com - 28 Nov 2007 21:56 GMT
> For an associative lookup, you will use one of the Map Collections. Java
> doesn't implement a 2D associative lookup so you have to do that yourself
[quoted text clipped - 12 lines]
> Curt Welch                                            http://CurtWelch.Com/
> c...@kcwc.com                                        http://NewsReader.Com/

Map<String, Map> values...
might work.
You'd have to use it like this I think (I haven't actually tried this
though):
values.getValue('a').getValue('b') to find 3 at "ab".
lord.zoltar@gmail.com - 28 Nov 2007 21:57 GMT
On Nov 28, 4:56 pm, lord.zol...@gmail.com wrote:
> > For an associative lookup, you will use one of the Map Collections. Java
> > doesn't implement a 2D associative lookup so you have to do that yourself
[quoted text clipped - 18 lines]
> though):
> values.getValue('a').getValue('b') to find 3 at "ab".

No... It would probably heave to be declared as:
Map<String, Map<String, int>> values = ...
Andreas Leitgeb - 28 Nov 2007 22:58 GMT
> On Nov 28, 4:56 pm, lord.zol...@gmail.com wrote:
>> > For an associative lookup, you will use one of the Map Collections. Java
[quoted text clipped - 8 lines]
> Map<String, Map<String, int>> values = ...
>> values.getValue('a').getValue('b') to find 3 at "ab".

If the OP does it that way, though, he must be aware that
adding new elements to nested HashMaps is somewhat more
complicated:  if the first key isn't yet there, it will
have to create a new nested HashMap bound to that first
key before he can insert the second key and set the value

Also, retrieving isn't just that simple:  The way you suggested
would throw NullPointerExceptions whenever the first key didn't
exist in the outer HashMap.

Unless the demands are too specific, I'd suggest either the
"make a string of both keys"-approach,  or if even that didn't
work out, to create a Pair<Keytype1,Keytype2> class, and use
that as Key to the HashMap:  HashMap<Pair<Keytype1,Keytype2>,Integer>
When creating the Pair-class, don't forget to override both its
.equals() and .hashCode() methods!
I'm not aware of any predefined general-purpose Pair class.
Stefan Ram - 28 Nov 2007 23:40 GMT
>Ideally I would have an array where I would have an input like
>Array[x] [y].getValue()

class P
{ final java.lang.Object[] value;
 public P( final java.lang.Object ... value ){ this.value = value; }
 public int hashCode(){ return java.util.Arrays.hashCode( value ); }
 public boolean equals( final java.lang.Object value )
 { return java.util.Arrays.equals
   ( this.value,(( P )value ).value ); }}

public class Main
{ public static P p(  final java.lang.Object ... value )
 { return new P( value ); }

 public static void main( final java.lang.String[] args )
 { final java.util.Map<P,java.lang.Integer> map
   = new java.util.HashMap<P,java.lang.Integer>();

   map.put( p( 'a', 'b' ), 3 );
   map.put( p( 'a', 'y' ), 5 );

   java.lang.System.out.println( map.get( p( 'a', 'b' )));
   java.lang.System.out.println( map.get( p( 'a', 'y' ))); }}

3
5
Stefan Ram - 29 Nov 2007 06:12 GMT
>map.put( p( 'a', 'b' ), 3 );
>map.put( p( 'a', 'y' ), 5 );
>java.lang.System.out.println( map.get( p( 'a', 'b' )));
>java.lang.System.out.println( map.get( p( 'a', 'y' )));

 One might remove the need for the above
 call to »p« to simplify the interface:

class P<E>
{ final E[] value;
 public P( final E ... value ){ this.value = value; }
 public int hashCode(){ return java.util.Arrays.hashCode( value ); }
 public boolean equals( final java.lang.Object value )
 { return value instanceof P ? java.util.Arrays.equals
   ( this.value,(( P )value ).value ): false; }}

interface MultiMap<K,V>
{ public void set( final V value, final K ... keys );
 public V get( final K ... keys ); }

class MultiHashMap<K,V> extends java.util.HashMap<P<K>,V>
implements MultiMap<K,V>
{ public void set( final V value, final K ... keys )
 { put( new P<K>( keys ), value ); }
 public V get( final K ... keys ){ return get( new P<K>( keys )); }}

public class Main
{ static final MultiMap<java.lang.Character,java.lang.Integer> map
 = new MultiHashMap<java.lang.Character,java.lang.Integer>();

 public static void main( final java.lang.String[] args )
 {
   map.set( 3, 'a', 'b' );
   map.set( 5, 'a', 'y' );

   java.lang.System.out.println( map.get( 'a', 'b' ));
   java.lang.System.out.println( map.get( 'a', 'y' )); }}

3
5
TheTravellingSalesman - 01 Dec 2007 05:44 GMT
So

Overall what everyone is suggesting that I should use Hashmaps, i.e.
my only way.

Another Lame way I though of was to have a String array which will
contain Strings like

> >map.put( p( 'a', 'b' ), 3 );
> >map.put( p( 'a', 'y' ), 5 );
[quoted text clipped - 36 lines]
> 3
> 5


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.