Java复式表

发布于 2024-09-05 08:47:10 字数 215 浏览 1 评论 0原文

有谁知道可以下载的 Java 中的复式表实现吗?

我需要做这样的事情

   1  2  3
   _______
a| x  y  z 

b| h  l  m

c| o  a  k

table.get(a,1) 会返回 x

当然,它应该使用任何对象作为键、值等

Does anyone know a double 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花落人离去 2024-09-12 08:47:10

有两种基本方法,具体取决于您的需求。

一种是创建一个 Hashtable(或类似的)Hashtable

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

另一种方法是构建您自己的数据类型来表示(整数,字符串)对,因此您可以执行以下操作:

Hashtable<YourFancyDatatype, String>

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>
山田美奈子 2024-09-12 08:47:10

你的问题的答案部分在于之前关于SO的问题:
Java 泛型 Pair存储在 HashMap 中无法正确检索键->值

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
  }
}

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
  }
}
说好的呢 2024-09-12 08:47:10

我假设您有一个字符/对象数组和一个数量,并且希望在表中相互交叉。您可以将每个字符映射到 0 .. qtyOfCharacters 之间的数字,并简单地创建一个二维数组 Object[][] table = new Object[A][B],其中 A 是刚刚映射的字符/对象的数量,B 是列的数量。

要将字符/对象映射到数字,您应该使用 HashMap/HashTable。

这个想法是,如果您访问“a,3”处的元素,您应该编写 table[ charmap.get("a")][ 3]

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文