简体   繁体   English

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

[英]How to add data from one ArrayList to another ArrayList using Java?

So I have an ArrayList "NamedShape" then i have another ArrayList "Connection" which consists of two fields both being "NamedShape" and i want to add data that i retrieve from the ArrayList "NamedShape" to the ArrayList "Connection" 所以我有一个ArrayList“ NamedShape”,然后我有另一个ArrayList“ Connection”,它由两个字段组成,都是“ NamedShape”,我想将从ArrayList“ NamedShape”检索到的数据添加到ArrayList“ Connection”

Here is my ArrayList "NamedShape" 这是我的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; 
    }
}

And here is my ArrayList "Connection" 这是我的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;
    }
   }

So i want to add data that i get from the ArrayList "NamedShape" and add it to the ArrayList "Connection". 所以我想添加从ArrayList“ NamedShape”获取的数据,并将其添加到ArrayList“ Connection”。 This is what i've used to retrieve data from the ArrayList "NamedSpace" but to add data to the "Connection" it doesn't work. 这就是我用来从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))));

}

Can you please help me with this? 你能帮我吗?

EDITED EDITED

So my code below is supposed to allow the user to draw a shape and when the shape is drawn, the user enters the name of the shape which is saved in the array "NamedShape". 因此,我下面的代码应允许用户绘制形状,并且在绘制形状时,用户输入形状的名称,该名称保存在数组“ NamedShape”中。 Then when the size of the "NamedShape is greater than one, it checks for collisions of other shapes with the line. Normally there should be only 2 collisions that is with the start of the line and the end of the line. If collisions exists with the line, it checks if collisions==1, it saved the shape that collides into ArrayList "Connection" under NamedShape1 and if collisions==2, it saved it to that same index but under "NamedShape2" Please do what you do best guys!! Help! 然后,当“ 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(); } } 

Why you are setting NameShape1 object before intiating it n the connection object? 为什么要在连接对象中初始化NameShape1对象之前设置它?

new NamedShape( con.setNameShape1(n,s) 新的NamedShape( con.setNameShape1(n,s)

Can you try initializing NameShape object and try to pass it to constructur of Connection? 您可以尝试初始化NameShape对象,然后尝试将其传递给Connection的构造方法吗?

it would be more on the below lines. 以下几行会更多。 con.add( new Connection(new NamedShape("abc", shapObj), new NamedShape("xyz",shapObj))); con.add(new Connection(new NamedShape(“ abc”,shapObj),new NamedShape(“ xyz”,shapObj)));

or see if you can initialize it in Connection object and one of method returns the given object. 或查看是否可以在Connection对象中对其进行初始化,并且其中一种方法返回给定的对象。

If you want to generate all the combinations among your NamedShape objects you can try with this : 如果要在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]));
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java,如何将一个对象添加到ArrayList并从另一个对象减少一个对象 - Java, how to add one object to an ArrayList and decrease one from another 如何在 Java 中将 Arraylist 添加到另一个 Arraylist 中 - How to add Arraylist into another Arraylist in Java 使用Java将数据从一维数组列表复制到二维数组列表 - Copy data from one dimensional arraylist to 2 dimensional arraylist using java 在java中将ArrayList添加到另一个ArrayList - Add ArrayList to another ArrayList in java 使用Java中的boolean方法将一个ArrayList与另一个ArrayList进行比较 - Comparing one ArrayList to another ArrayList using boolean method in java Java OOP 无法将特定元素从 arraylist 添加到另一个 ZEB3A834246F50C5BDE2C825D7 - Java OOP not able to add specific element from arraylist to another arraylist 从一个ArrayList中查找一个字符串,作为Java 8中另一个ArrayList字符串的字符序列 - Find a String from one ArrayList as a Charsequence of another ArrayList String in Java 8 Java:将元素从一个ArrayList传输到另一个ArrayList - Java: transfering elements from one ArrayList to another ArrayList 将一项从 ArrayList 复制到另一个 ArrayList Java 8 - Copy one item from an ArrayList to another ArrayList Java 8 如何将对象从ArrayList添加到另一个类中的另一个ArrayList - How to add an object from an ArrayList to another ArrayList in another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM