2010-06-10 4 views
3

Java에서 double entry table 구현을 아는 사람이 있습니까?Java double entry table

가 나는 등,이 키 값으로 개체를 사용해야합니다, 물론 x

반환이

1 2 3 
    _______ 
a| x y z 

b| h l m 

c| o a k 

table.get(a,1) 같은 것을 할 필요가

+0

aw! 가을 학기에 구아바 프로젝트를 확인하십시오. 테이블 수업을 듣기에 매우 행복합니다. http://guava-libraries.googlecode.com –

답변

3

두 가지 기본 방법이 있습니다, 당신의 필요에 따라.

Hashtable (또는 이와 유사한 것)을 Hashtables로 만드는 것 중 하나입니다. 난 당신이 문자/객체의 배열을 가지고 있으리라 믿고있어

Hashtable<YourFancyDatatype, String> 
+0

+1 댕, 저를 때려주십시오 – Jacob

+2

__YourFancyDataType__을 사용하는 경우 __hashCode__ 및 __equals__ 메서드의 일관된 구현을 제공해야합니다. – Jack

+0

Java 해시 테이블에서 HashTable이 아니라 HashTable입니다. –

0

을 :

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

또 다른 방법은 (정수, 문자열) 쌍을 나타냅니다 자신의 데이터 유형을 구축하는 것입니다, 그래서 당신은 할 수있다 수량과 당신의 테이블에서 서로 교차하고 싶다. 각 문자를 0 .. qtyOfCharacters 사이의 숫자로 매핑하고 양방향 배열을 만들면됩니다. Object [] [] table = new Object [A] [B], 여기서 A는 방금 매핑 된 문자/개체의 양이며 B는 열의 수

문자/개체를 숫자로 매핑하려면 HashMap/HashTable을 사용해야합니다.

아이디어는 당신의 요소에 액세스하는 경우 "는, 3"당신이 이전에 테이블 [charmap.get ("A")] [3]

1

질문에 대한 대답은 부분적으로 거짓말을 작성해야한다는 것입니다 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 
    } 
}