gpt4 book ai didi

java - 如何使用Java将数据从一个ArrayList添加到另一个ArrayList?

转载 作者:行者123 更新时间:2023-12-01 11:49:54 25 4
gpt4 key购买 nike

所以我有一个ArrayList“NamedShape”,然后我有另一个ArrayList“Connection”,它由两个字段组成,都是“NamedShape”,我想将从ArrayList“NamedShape”检索到的数据添加到ArrayList“Connection”

这是我的 ArrayList“NamedShape”

ArrayList<NamedShape> shapes = new ArrayList<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;
}
}

这是我的 ArrayList“连接”

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“Connection”。这是我用来从 ArrayList“NamedSpace”检索数据的方法,但将数据添加到“Connection”时则不起作用。

for(int a=0;a<var;a++) 
{
Shape s = shapes.get(a).getShape();
String n = shapes.get(a).getName();

// add data to ArrayList "Connection"
//I've tried this but it does not work.

con.add( new Connection(new NamedShape(con.setNameShape1(n,s))));

}

你能帮我解决这个问题吗?

已编辑

所以我下面的代码应该允许用户绘制形状,并且当绘制形状时,用户输入保存在数组“NamedShape”中的形状的名称。然后,当“NamedShape”的大小大于 1 时,它会检查其他形状与该线的碰撞。通常应该只有 2 次碰撞,即与线的开头和线的结尾。如果存在与该行,它检查是否碰撞==1,它保存碰撞到NamedShape1下的ArrayList“Connection”的形状,如果碰撞==2,它保存它到相同的索引,但在“NamedShape2”下请做你最擅长的事!!救命!

if (currentAction == 3) {
boolean collision = false;
int collisions =0;
aShape = drawEllipse(drawStart.x, drawStart.y,
e.getX(), e.getY());


String text = (String) JOptionPane.showInputDialog(DrawingBoard, "Enter name of Attribute:");
if (text == null || text.isEmpty()) {
text = (String) JOptionPane.showInputDialog(DrawingBoard, "You must enter a valid name! Please try again:");

}
else{


shapes.add( new NamedShape( text,
aShape ) );


//returns the coordinates of each rectangle
//System.out.println(aShape);
int var = shapes.size();
System.out.println("Array index"+var);
if(var>1){

for(int i=0;i<var;i++){

Shape s = shapes.get(i).getShape();

if (s instanceof Line2D) {

System.out.println("Index of line is: "+i);
double x = (s.getBounds2D().getX());
double y = (s.getBounds2D().getY());
double w = (s.getBounds2D().getWidth());
double h = (s.getBounds2D().getHeight());

for(int a=0;a<var;a++) {
Shape f = shapes.get(a).getShape();
String nana = shapes.get(a).getName();


if (f instanceof Ellipse2D){
double x1 = (f.getBounds2D().getX());
double y1 = (f.getBounds2D().getY());
double w1 = (f.getBounds2D().getWidth());
double h1 = (f.getBounds2D().getHeight());

if(s.intersects(x1, y1, w1, h1)){
collision = true;
collisions ++;
if (collisions==1) {

// I want to add data of f and nana from arraylist of NamedShape and adds it
//to arraylist connection.
// if collsions=1, it adds it to NamedShape1 else if collisions = 2 it add it to NamedShape2

//not sure if this will work
con.add( new Connection(new NamedShape(nana, f), new NamedShape(null,null)));

}

System.out.println("Line "+ i +" is linked to "+nana);

}
else{
collision = false;
System.out.println("LINE(E) " + i + "DO NOT COLLIDE WITH ELLIPSE OF INDEX= "+a);

}

}

}

}

}
}

shapeStroke.add(strokeColor);
drawStart = null;
drawEnd = null;
repaint();
}

}

最佳答案

为什么要在连接对象中启动 NameShape1 对象之前设置它?

new NamedShape(con.setNameShape1(n,s)

您可以尝试初始化 NameShape 对象并将其传递给 Connection 的构造函数吗?

下面几行会更多。con.add( new Connection(new NamedShape("abc", shapObj), new NamedShape("xyz",shapObj)));

或者看看是否可以在 Connection 对象中初始化它,并且其中一个方法返回给定的对象。

关于java - 如何使用Java将数据从一个ArrayList添加到另一个ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28877578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com