[英]How Can I make 2D array with different object of class?
I have three classes.The first class is Node that has 2 variables and the second class is Instance with two variables.The third class which is called Decision has two 2-D arraies the fist one is an 2D array with different types of classes. 我有3个类,第一个类是具有2个变量的Node,第二个类是具有两个变量的Instance。第三个类,称为Decision具有两个2-D数组,第一个是具有不同类型的类的2D数组。 How Can I make 2D array with different objects of classes(Nodes and Instances)?
如何制作具有不同类对象(节点和实例)的2D阵列? The second 2D array(y) is type Integer which each dimension is a variable of class Node and class Intances.
第二个2D数组(y)是Integer类型,其每个维都是Node类和Intances类的变量。 How can I say for example y[0][0]=1
我怎么说?例如y [0] [0] = 1
public class Nodes { 公共类节点{
private int nodeID;
private String nodeType;
public Nodes(int nodeID, String nodeType) {
this.nodeID = nodeID;
this.nodeType = nodeType;
}
public int getNodeID() {
return nodeID;
}
public void setNodeID(int nodeID) {
this.nodeID = nodeID;
}
public String getNodeType() {
return nodeType;
}
public void setNodeType(String nodeType) {
this.nodeType = nodeType;
}
} }
public class Instances { 公共类实例{
private int type;
private int traffic;
public Instances(int Type, int traffic) {
this.type = type;
this.traffic = traffic;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getTraffic() {
return traffic;
}
public void setTraffic(int traffic) {
this.traffic = traffic;
}
} }
public class Decision { 公共类决定{
Nodes node1=new Nodes();
Instances ins1= new Instances();
private Nodes Instances x[][];
private int [][] y=new int [node1.getNodeID()][ins1.gettype()];
//private Nodes[] node;
//private Instances[] ins;
} }
simply you can't , the multi dimensional arrays are most to be considered as an array of array . 简直不能,多维数组最应该被视为数组的数组。
How Can I make 2D array with different objects of classes(Nodes and Instances)?
如何制作具有不同类对象(节点和实例)的2D阵列?
I think it will be easy to do something very similar with java List
and Map
,it does have all the classes you need to go with . 我认为使用Java
List
和Map
非常类似的操作会很容易,它确实具有您需要使用的所有类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.