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

How to add data from one ArrayList to another ArrayList using Java?

所以我有一个 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 "Connection"

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 次碰撞,即线的起点和线的终点。如果碰撞存在于该行,它检查是否 collisions==1,它将碰撞的形状保存到 NamedShape1 下的 ArrayList "Connection" 中,如果 collisions==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();
                            }
                           
                        }

如果您想生成 NamedShape 对象之间的所有组合,您可以尝试这样做:

for(int i=0; i<shapes.size()-1; i++){
 for(int j=i+1; j<shapes.size();j++){
    conn.add(new Connection(shapes[i],shapes[j]));
}
}

为什么在连接对象中初始化 NameShape1 对象之前设置它?

new NamedShape(con.setNameShape1(n,s)

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

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

或者查看是否可以在 Connection 对象和方法之一中初始化它 returns 给定对象。