Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对于这个Java问题,我应该使用哪种类型的集合?_Java_Collections - Fatal编程技术网

对于这个Java问题,我应该使用哪种类型的集合?

对于这个Java问题,我应该使用哪种类型的集合?,java,collections,Java,Collections,我想要一个只包含整数的二维数组。坐标也是整数 问题是: 我事先不知道会有多少列 我想随机填一下 例如: 假设第一个数字是2,放在(3;2)中。我希望能够简单地添加8,例如:array.add(8,2,1) 我已经找到了一些确实有效的东西,但它真的很重。是的 Hashtable<Integer, Hashtable<Integer, Integer>> 哈希表 有人看到一种更复杂的方法吗?我不太擅长收藏。请查看tintintthashmapin。那些藏品非常好。请在

我想要一个只包含整数的二维数组。坐标也是整数

问题是:

  • 我事先不知道会有多少列
  • 我想随机填一下
例如:

假设第一个数字是
2
,放在
(3;2)
中。我希望能够简单地添加
8
,例如:
array.add(8,2,1)

我已经找到了一些确实有效的东西,但它真的很重。是的

Hashtable<Integer, Hashtable<Integer, Integer>>
哈希表

有人看到一种更复杂的方法吗?我不太擅长收藏。

请查看
tintintthashmap
in。那些藏品非常好。

请在中查看
tintintthashmap
。这些藏品非常好。

制作一个适合您需要的类。或者更好的方法是,创建一个接口,声明要在其中使用类的方法。在代码中,请参考接口的类型


在接口的实现中,我将使用
ArrayList
。在读取或分配数字之前,只需检查坐标是否有效。如果不是,在设置或读取值之前,您应该能够使用null(或其他非数字值)预填充数组。

创建一个适合您需要的类。或者更好的方法是,创建一个接口,声明要在其中使用类的方法。在代码中,请参考接口的类型


在接口的实现中,我将使用
ArrayList
。在读取或分配数字之前,只需检查坐标是否有效。如果没有,在设置或读取值之前,您应该能够使用null(或其他非数字值)预填充数组。

您不能创建一个具有字符串键的哈希映射并存储您想要的任何数值吗?即

Map<String, Integer> m = new HashMap<String,Integer>();
m.put(makeKey(3,2), 2); // where makeKey returns something like "[3,2]"
Map m=newhashmap();
m、 put(makeKey(3,2),2);//其中makeKey返回类似“[3,2]”的内容
编辑:


缺点是你没有一个可靠的迭代顺序,也就是说,如果你想迭代第n行或第n列中的所有内容,没有简单的方法可以有效地做到这一点。

你不能创建一个具有字符串键的哈希映射并存储你想要的任何数值吗?即

Map<String, Integer> m = new HashMap<String,Integer>();
m.put(makeKey(3,2), 2); // where makeKey returns something like "[3,2]"
Map m=newhashmap();
m、 put(makeKey(3,2),2);//其中makeKey返回类似“[3,2]”的内容
编辑:


缺点是您没有可靠的迭代顺序,也就是说,如果您想迭代第n行或第n列中的所有内容,没有简单的方法可以有效地做到这一点。

我猜您应该查看二维数组或数组列表,因为您不知道它们的大小。

我猜您应该查看二维数组或数组列表,因为您不知道它们的大小。

整数列表列表可以工作

List<List<Integers>

List整数列表列表可以工作

List<List<Integers>

List也许你们可以用番石榴的桌子
这将为您提供table.put(R行键、C列键、V值)


也许你们可以用番石榴做的桌子 这将为您提供table.put(R行键、C列键、V值)


在这种情况下,您需要一些定制的类。如何定义一个单元类:

public class Cell implements Comparable<Cell> {
    public int row;
    public int col;

    public Cell() {
        this(0, 0);
    }

    public Cell(int row, int col) {
        this.row = row;
        this.col = col;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + this.row;
        result = prime * result + this.col;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instance of Cell))
            return false;
        Cell cell = (Cell) obj;
        return this.row == cell.row && this.col == cell.col;
    }

    //Define ordering for cells
    @Override
    public int compareTo(Cell cell) {
        int compare = cell.row - this.row;
        if (compare == 0)
            compare = cell.col - this.col;
        return compare;
    }
}
公共类单元格实现可比较{
公共int row;
公共int col;
公共单元格(){
这个(0,0);
}
公共单元格(整数行,整数列){
this.row=行;
this.col=col;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+this.row;
结果=素数*结果+此.col;
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
if(!(单元格的obj实例))
返回false;
Cell=(Cell)obj;
返回this.row==cell.row&&this.col==cell.col;
}
//定义单元格的顺序
@凌驾
公共整数比较(单元格){
int compare=cell.row-this.row;
如果(比较==0)
比较=cell.col-this.col;
返回比较;
}
}
然后是一个网格类,它扩展TreeMap以维护逻辑单元顺序:

import java.util.TreeMap;

public class Grid extends TreeMap<Cell, Integer> {
    public Integer get(int row, int col) {
        return this.get(new Cell(row, col));
    }

    public Integer put(int row, int col, Integer value) {
        return this.put(new Cell(row, col), value);
    }
}
import java.util.TreeMap;
公共类网格扩展树映射{
公共整数get(整数行,整数列){
返回这个.get(新单元格(行,列));
}
公共整数put(整数行、整数列、整数值){
返回此.put(新单元格(行、列)、值);
}
}

在这种情况下,您需要一些定制的类。如何定义一个单元类:

public class Cell implements Comparable<Cell> {
    public int row;
    public int col;

    public Cell() {
        this(0, 0);
    }

    public Cell(int row, int col) {
        this.row = row;
        this.col = col;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + this.row;
        result = prime * result + this.col;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instance of Cell))
            return false;
        Cell cell = (Cell) obj;
        return this.row == cell.row && this.col == cell.col;
    }

    //Define ordering for cells
    @Override
    public int compareTo(Cell cell) {
        int compare = cell.row - this.row;
        if (compare == 0)
            compare = cell.col - this.col;
        return compare;
    }
}
公共类单元格实现可比较{
公共int row;
公共int col;
公共单元格(){
这个(0,0);
}
公共单元格(整数行,整数列){
this.row=行;
this.col=col;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+this.row;
结果=素数*结果+此.col;
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
if(!(单元格的obj实例))
返回false;
Cell=(Cell)obj;
返回this.row==cell.row&&this.col==cell.col;
}
//定义单元格的顺序
@凌驾
公共整数比较(单元格){
int compare=cell.row-this.row;
如果(比较==0)
比较=cell.col-this.col;
返回比较;
}
}
然后是一个网格类,它扩展TreeMap以维护逻辑单元顺序:

import java.util.TreeMap;

public class Grid extends TreeMap<Cell, Integer> {
    public Integer get(int row, int col) {
        return this.get(new Cell(row, col));
    }

    public Integer put(int row, int col, Integer value) {
        return this.put(new Cell(row, col), value);
    }
}
import java.util.TreeMap;
公共类网格扩展树映射{
公共整数get(int行,