如何用另一个 ArrayList 的对象创建一个 ArrayList?
How to create an ArrayList with objects of another ArrayList?
所以我有一个 ArrayList "NamedShape"
ArrayList<NamedShape> shapes = new ArrayList<NamedShape>();
包含一个 Shape 和一个字符串,我想要的是另一个 ArrayList "Connection" 将包含两个字段,但两者都是 "NamedShape"。我的 ArrayList "Connection" 将如下所示:
这是我的 class "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;
}
}
这是我的连接class:
public class Connection {
private NamedShape namedShape1;
private NamedShape namedShape2;
public Connection(??){
??
}
}
你能帮我创建这个新的 ArrayList "Connection" 吗?
您可以创建另一个包含两个 NamedShape 对象的 class。
public class Connection {
private NamedShape namedShapeOne;
private NamedShape namedShapeTwo;
.............
.............
.............
}
现在根据需要创建数组列表
List<Connection> connectionList = new ArrayList<Connection>();
新建 class Connection
-
public class Connection {
private NamedShape namedShape;
//constructors
//getter setters
}
然后您可以创建 Connection
-
的新数组列表
List<Connection> connections = new ArrayList<Connection>();
您的问题不言自明。如果您创建一个 ArrayList<Connection>
,这意味着您已经有一个 Connection
class,其中您有两个 NamedShape 对象的引用。因此,正如前面的答案所建议的那样,您只需要声明一个 Connection
class.
如果您不想使用连接 class,您的新 ArrayList 将如下所示:
ArrayList<ArrayList<NamedShape>>
即一个ArrayList
包含ArrayList
个包含NamedShape
个
谢谢大家,我做到了。 :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;
}
}
所以我有一个 ArrayList "NamedShape"
ArrayList<NamedShape> shapes = new ArrayList<NamedShape>();
包含一个 Shape 和一个字符串,我想要的是另一个 ArrayList "Connection" 将包含两个字段,但两者都是 "NamedShape"。我的 ArrayList "Connection" 将如下所示:
这是我的 class "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;
}
}
这是我的连接class:
public class Connection {
private NamedShape namedShape1;
private NamedShape namedShape2;
public Connection(??){
??
}
}
你能帮我创建这个新的 ArrayList "Connection" 吗?
您可以创建另一个包含两个 NamedShape 对象的 class。
public class Connection {
private NamedShape namedShapeOne;
private NamedShape namedShapeTwo;
.............
.............
.............
}
现在根据需要创建数组列表
List<Connection> connectionList = new ArrayList<Connection>();
新建 class Connection
-
public class Connection {
private NamedShape namedShape;
//constructors
//getter setters
}
然后您可以创建 Connection
-
List<Connection> connections = new ArrayList<Connection>();
您的问题不言自明。如果您创建一个 ArrayList<Connection>
,这意味着您已经有一个 Connection
class,其中您有两个 NamedShape 对象的引用。因此,正如前面的答案所建议的那样,您只需要声明一个 Connection
class.
如果您不想使用连接 class,您的新 ArrayList 将如下所示:
ArrayList<ArrayList<NamedShape>>
即一个ArrayList
包含ArrayList
个包含NamedShape
个
谢谢大家,我做到了。 :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;
}
}