所以我有一个ArrayList“NamedShape”
ArrayList<NamedShape> shapes = new ArrayList<NamedShape>();
包含一个Shape和一个String,我想要的是另一个ArrayList“Connection”,它将包含两个字段,但都是“NamedShape”。我的ArrayList“Connection”将如下所示:
这是我的“NamedShape”课程:
public class NamedShape {
private String name;
private Shape shape;
public NamedShape( String name, Shape shape ){
this.name = name;
this.shape = shape;
}
public String getName(){
return name;
}
public Shape getShape(){
return shape;
}
}
这是我的Connection类:
public class Connection {
private NamedShape namedShape1;
private NamedShape namedShape2;
public Connection(??){
??
}
}
你能帮我创建一个新的ArrayList“Connection”吗?
答案 0 :(得分:2)
您可以创建另一个包含两个NamedShape对象的类。
public class Connection {
private NamedShape namedShapeOne;
private NamedShape namedShapeTwo;
.............
.............
.............
}
现在根据需要创建数组列表
List<Connection> connectionList = new ArrayList<Connection>();
答案 1 :(得分:1)
创建新课程Connection
-
public class Connection {
private NamedShape namedShape;
//constructors
//getter setters
}
然后您可以创建Connection
-
List<Connection> connections = new ArrayList<Connection>();
答案 2 :(得分:1)
你的问题是自我解释。如果您创建ArrayList<Connection>
,则表示您已经拥有Connection
类,其中您有两个NamedShape对象的引用。因此,正如先前的答案所示,您只需要声明Connection
类。
如果你想没有Connection类,你的新ArrayList将如下所示:
ArrayList<ArrayList<NamedShape>>
即包含ArrayList
s ArrayList
的{{1}}。
答案 3 :(得分:1)
谢谢你们,我能够做到。 :d
ArrayList<Connection> con = new ArrayList<Connection>();
public class Connection {
private NamedShape namedShape1;
private NamedShape namedShape2;
public Connection(NamedShape namedShape1,NamedShape namedShape2){
this.namedShape1 = namedShape1;
this.namedShape2 = namedShape2;
}
public NamedShape getNamedShape1(){
return namedShape1;
}
public NamedShape getNamedShape2(){
return namedShape2;
}
public void setNameShape1(){
this.namedShape1 = namedShape1;
}
public void setNameShape2(){
this.namedShape2 = namedShape2;
}
}