Java 如何创建行和列的字符串索引以及单元格的布尔值表?
我需要一个可以解决以下问题的java程序: 1-它有一个数据结构(DS)来表示以下数据,其中行和列由字符串索引,单元格值为布尔值。 因此,要访问第(i)行,我可以简单地说DS[“Yi”],要访问第(i)行中的特定单元(j),我可以说DS[“Yi”,“Xj”] 2-必须从类字段填充列索引{“X1”、“X2”、“X3”、…、“Xn”}。例如,包括以下类别:Java 如何创建行和列的字符串索引以及单元格的布尔值表?,java,data-structures,Java,Data Structures,我需要一个可以解决以下问题的java程序: 1-它有一个数据结构(DS)来表示以下数据,其中行和列由字符串索引,单元格值为布尔值。 因此,要访问第(i)行,我可以简单地说DS[“Yi”],要访问第(i)行中的特定单元(j),我可以说DS[“Yi”,“Xj”] 2-必须从类字段填充列索引{“X1”、“X2”、“X3”、…、“Xn”}。例如,包括以下类别: public class Test { private String X1; private String X2; p
public class Test {
private String X1;
private String X2;
private String X3;
private String X4;
private String X5;
}
对于这个类,我的表的列将是{“X1”、“X2”、“X3”、“X4”、“X5”},如果我稍后更新类测试以包含一个更多的字段,比如“X6”,那么DS必须自动包含这个新字段
3-最后,我想将这些数据保存到一个文件{TXT、XML或JSON}中,这样每次代码运行时,它都可以从文件中读取值。我认为最简单的方法可能是使用某种约定,您可以在内部将已知的行和列标签转换为数字索引。然后,您可以使用一个普通的2D布尔数组 如果你不能做到这一点,那么一个选择就是使用地图地图,比如:
Map<String, Map<String, Boolean>> grid = new HashMap<>();
// populate first row
grid.put("Y1", new HashMap<>());
grid.get("Y1").put("X1", true);
grid.get("Y1").put("X2", true);
grid.get("Y1").put("X3", false);
// ... other columns
grid.get("Y1").put("Xn", true);
Map grid=newhashmap();
//填充第一行
put(“Y1”,新的HashMap());
grid.get(“Y1”).put(“X1”,true);
grid.get(“Y1”).put(“X2”,true);
grid.get(“Y1”).put(“X3”,false);
// ... 其他栏目
grid.get(“Y1”).put(“Xn”,true);
尝试一下这种方法
注意:此代码未经测试。在这里,我使用了数组上的int-index搜索,因为我将假设列表(例如“x1”、“x2”、“x3”
)不一定要排列(可能您可以尝试使用映射
)
公共类SS{
公共静态void main(字符串[]args){
//用于索引
List listX=数组。asList(“x1”、“x2”、“x3”);
listY=Arrays.asList(“y1”、“y2”、“y3”);
//用于获取索引为arr[x][y]的布尔值,该值在类YourClass中定义
YourClass[]arr=newYourClass[listX.size()][listY.size()];
int i=0;
for(字符串y:listY){
int j=0;
for(字符串x:listX){
//填充数组
arr[i][j]=newyourclass(newrandom().nextInt(1),x,y);
j++;
}
i++;
}
//要获得DS[“x2”,“y3”]
DS=新DS(“x2”、“y3”);
i=0;
for(字符串y:listY){
如果(ds.getY()等于(y))
{
int j=0;
for(字符串x:listX){
如果(ds.getX()等于(x))
System.out.println(arr[i][j].toString());
j++;
}
}
i++;
}
}
}
//类来维持指数位置
你的班级{
INTA;
字符串x;
弦y;
公共类(int a、字符串x、字符串y){
这个a=a;
这个.x=x;
这个。y=y;
}
@凌驾
公共字符串toString(){
返回“YourClass{”+
“a=”+a+
“,x=”+x+“\”+
“,y=”+y+“\”+
'}';
}
公共int getA(){
返回a;
}
公共无效setA(INTA){
这个a=a;
}
公共字符串getX(){
返回x;
}
公共void setX(字符串x){
这个.x=x;
}
公共字符串getY(){
返回y;
}
公共void setY(字符串y){
这个。y=y;
}
}
//用于搜索元素的类
DS类{
字符串x;
弦y;
公共字符串getX(){
返回x;
}
公共void setX(字符串x){
这个.x=x;
}
公共字符串getY(){
返回y;
}
公共void setY(字符串y){
这个。y=y;
}
公共DS(字符串x、字符串y){
这个.x=x;
这个。y=y;
}
}
感谢您的回复,但是,我想这段代码的动态性与第2点所述有所不同,谢谢您的回复。地图地图可以工作,但是,它在内存消耗方面是昂贵的,因为它需要每一行,我必须重新存储列标题。
public class SS {
public static void main(String[] args) {
//Used for indexing
List<String> listX = Arrays.asList("x1", "x2", "x3");
List<String> listY = Arrays.asList("y1", "y2", "y3");
//Used to fetch boolean value which is indexed arr[x][y] with the value defined in the class YourClass
YourClass[][] arr = new YourClass[listX.size()][listY.size()];
int i=0;
for (String y : listY) {
int j=0;
for (String x : listX) {
//Fill the array
arr[i][j] = new YourClass(new Random().nextInt(1), x, y);
j++;
}
i++;
}
//To get DS["x2", "y3"]
DS ds = new DS("x2", "y3");
i=0;
for (String y : listY) {
if(ds.getY().equals(y))
{
int j=0;
for (String x : listX) {
if(ds.getX().equals(x))
System.out.println(arr[i][j].toString());
j++;
}
}
i++;
}
}
}
//class to maintain index positions
class YourClass{
int a;
String x;
String y;
public YourClass(int a, String x, String y) {
this.a = a;
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "YourClass{" +
"a=" + a +
", x='" + x + '\'' +
", y='" + y + '\'' +
'}';
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
}
//class used to search the element
class DS {
String x;
String y;
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
public DS(String x, String y) {
this.x = x;
this.y = y;
}
}