How to code something in Java

  • Thread starter TheTravellingSalesman
  • Start date
T

TheTravellingSalesman

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.
 
L

lord.zoltar

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.

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.
 
R

rossum

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.
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
 
C

Curt Welch

TheTravellingSalesman said:
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.

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");
 
L

lord.zoltar

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");

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".
 
L

lord.zoltar

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".

No... It would probably heave to be declared as:
Map<String, Map<String, int>> values = ...
 
J

Joshua Cranmer

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.
 
A

Andreas Leitgeb

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.
 
S

Stefan Ram

TheTravellingSalesman said:
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
 
S

Stefan Ram

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
 
T

TheTravellingSalesman

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


[email protected] (Stefan Ram) said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top