2009-10-14 17 views

回答

1

我假设你需要Java中的数据结构来存储这些值。

您可以使用以下语法在Java中定义2D数组。

String[][] strArr = new String[5][5]; //defines a 5*5 String array 
String[][] strArr2 = new String[1][2]; //defines a 1*2 String array 

请注意,数组只能保存1个数据类型的值。具体的项目可以使用类似

System.out.println(strArr2[0][1]); 

针对您的特殊例子来间接引用,你也可以使用java.util.Map类和数据存储为键值对,但是这需要“钥匙”是唯一的。例如,

Map<String,Integer> keyval = new HashMap<String, Integer>(); 
keyval.put("B",1); 
keyval.put("C",2); 
keyval.put("D",3); 
keyval.put("D",4); //wrong. will overwrite the previous entry. 
2

如果你希望做一个电子表格应用程序类型(推断,可能是错误的,从弹簧框架标签和B-1/C-2名),稀疏阵列是可能的要走的路。

Colt有一个这样的实现。

我回答了类似的问题here

0

我猜你需要的是一个地图的ArrayList的:

HashMap<String, ArrayList<YourClass>> map; 

这样,B-1将map.get( “B”)得到(1);

4

有三种基本类型的多维数组的:

  1. 固定。想象一下棋盘,它总是8x8(尽管有变种)。但是这也可能意味着可变的东西,但在实例化时会固定;
  2. 稀疏。数组之间存在大量空值空间。电子表格通常适合这种描述,但是什么是和不是稀疏的通常是一种主观判断;和
  3. 密集。稀疏的相反。使用大部分或全部可能的值。

Java没有本机多维数组类型。 Java有数组的数组,这是不完全相同的事情。例如,这是合法的Java:

int arr[][] = new int[] { 
    new int[3], 
    new int[4], 
    new int[5] 
}; 

固定阵列可以这样做,但它可能会很尴尬。它往往更容易使用的一维阵列与对象包装:

public class Chessboard { 
    public final static DEFAULT_X = 8; 
    public final static DEFAULT_Y = 8; 
    public final static DEFAULT_SIZE = DEFAULT_X * DEFAULT_Y; 

    private final int x; 
    private final int y; 
    private final int size; 
    private final Piece squares[]; 

    public Chessboard() { 
    this(DEFAULT_X, DEFAULT_Y); 
    } 

    public Chessboard(int x, int y) { 
    if (x < 2) { 
     throw new IllegalArgumentException("x (" + x + ") must be 2+"); 
    } 
    if (y < 2) { 
     throw new IllegalArgumentException("y (" + y + ") must be 2+"); 
    } 
    this.x = x; 
    this.y = y; 
    size = x * y; 
    pieces = new Piece[size]; 
    } 

    public Piece get(int x, int y) { 
    return pieces[y * this.x + x]; 
    } 

    public Piece get(String xy) { 
    // eg 'h3' => (7,2) 
    return get(xy.charAt(0) - 'a', xy.charAt(1) - '0'); 
    } 

    // etc 
} 

当然这可以用数组的数组代替来完成。

稀疏阵列往往用Java实现与地图:

public class Sparse2DArray<T> { 
    public final static int MAX_X = 8192; 
    public final static int MAX_Y = 8192; 

    private final Map<String, T> array = new HashMap<String, T>(); 
    private final Pattern XY = Pattern.compile("^([A-Za-z]+)([0-9]+)"); 

    public T get(int x, int y) { 
    if (x < 0 || x >= MAX_X) { 
     throw new IllegalArgumentException("x (" + x + ") must be 0 to " + (MAX_X-1)); 
    } 
    if (y < 0 || y >= MAX_Y) { 
     throw new IllegalArgumentException("y (" + y + ") must be 0 to " + (MAX_Y-1)); 
    } 
    return array.get(x + "," + y); 
    } 

    public T get(String xy) { 
    Matcher m = XY.matcher(xy); 
    if (!m.matches()) { 
     throw new IllegalArgumentException("xy (" + xy + ") must be letters followed by digits"); 
    } 
    String s = m.group(1).toUpperCase(); 
    int multiplier = 1; 
    int x = 0; 
    for (int i=s.length()-1; i>=0; i--) { 
     x += (s.chartAt(i) - 'A') * multiplier; 
     multiplier *= 26; 
    } 
    int y = Integer.valueOf(m.group(2)); 
    return array.get(x, y); 
    } 

    // etc 
} 

您也可以通过构建一个N维键类的映射键使用做到这一点。它需要定义适当的equals()hashCode()方法,并且可能会是一个更清洁的解决方案,但上述方法仍然有效。

很明显,如果你打算在稀疏数组类中存储100x100数组的每个值,那么会有开销(因为所有的地图都有开销)。这带来了密集阵列。稀疏数组仅存储设置值的值。密集数组为每个可能的键(在指定范围内)存储一个值。

此外密集阵列与固定阵列类似但不完全相同。固定数组一旦创建就难以扩展(当然,这是一项昂贵的操作,因此通常不允许),而密集数组可能会被扩展,就像您可以将它们用于电子表格并扩展max X和Y值是用户使用越来越多的单元格,因此您可以定义一个包含所有使用的值的矩形。

+0

OMG,那是什么..:o +1本论文 – 2009-10-14 13:47:05