开发者

Java double entry table

Does anyone know a d开发者_JAVA百科ouble entry table implementation in Java I can download ?

I need to do something like this

   1  2  3
   _______
a| x  y  z 

b| h  l  m

c| o  a  k

table.get(a,1) would return x

Of course, it should use any Object as key, value, etc


There are two basic approaches, depending on your needs.

One is to make a Hashtable (or similar) of Hashtables.

Hashtable<Integer, Hashtable<String, String>> = ...;

Another approach is to build your own datatype that represents an (Integer, String) pair, so you can do:

Hashtable<YourFancyDatatype, String>


The answer to your question partially lies in a previous questions on SO: Java generics Pair<String, String> stored in HashMap not retrieving key->value properly

import java.lang.*; 
import java.util.*; 

public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB> > {
  protected final TYPEA Key_;
  protected final TYPEB Value_;

  public Pair(TYPEA key, TYPEB value) {
    Key_   = key;
    Value_ = value;
  }
  public TYPEA getKey() {
    return Key_;
  }
  public TYPEB getValue() {
    return Value_;
  }
  public String toString() {
    System.out.println("in toString()");
    StringBuffer buff = new StringBuffer();
    buff.append("Key: ");
    buff.append(Key_);
    buff.append("\tValue: ");
    buff.append(Value_);
    return(buff.toString() );
  }
  public int compareTo( Pair<TYPEA, TYPEB> p1 ) { 
    System.out.println("in compareTo()");
    if ( null != p1 ) { 
       if ( p1.equals(this) ) { 
          return 0; 
       } else if ( p1.hashCode() > this.hashCode() ) { 
          return 1;
       } else if ( p1.hashCode() < this.hashCode() ) { 
          return -1;  
       }
    }
    return(-1);
  }

  public int hashCode() { 
    int hashCode = Key_.hashCode() + (31 * Value_.hashCode());
    System.out.println("in hashCode() [" + Integer.toString(hashCode) + "]");
    return(hashCode);
  }

  @Override
  public boolean equals(Object o) { 
      System.out.println("in equals()");
      if (o instanceof Pair) { 
         Pair<?, ?> p1 = (Pair<?, ?>) o;
         if ( p1.Key_.equals( this.Key_ ) && p1.Value_.equals( this.Value_ ) ) { 
            return(true);
         }
      }
      return(false);
  }

  public static void main(String [] args) {
     HashMap< Pair<String, int>, String> table = new HashMap<Pair<String,int>, String>();
     table.put(new Pair<String, int>("a", 1), "x");
     table.put(new Pair<String, int>("a", 2), "y");
     table.put(new Pair<String, int>("a", 3), "z");
     table.put(new Pair<String, int>("b", 1), "h");
     table.put(new Pair<String, int>("b", 2), "l");
     table.put(new Pair<String, int>("b", 3), "m");
     table.put(new Pair<String, int>("c", 1), "o");
     table.put(new Pair<String, int>("c", 2), "a");
     table.put(new Pair<String, int>("c", 3), "k"); 

     String val = table.get(new Pair<String, int>("a", 1)); //val is x for this input pair
  }
}


I'm assuming you have an array of characters/objects and a quantity and want cross each other in your table. You could map each character to a number between 0 .. qtyOfCharacters and simply create a bidimensional array Object[][] table = new Object[A][B], where A is the quantity of characters/object just mapped, and B is the quantity of columns.

To map the characters/object to numbers you should use a HashMap/HashTable.

The idea is that if you access the element at "a,3" you should write table[ charmap.get("a")][ 3]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜