简体   繁体   中英

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"

Here is my 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<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". This is what i've used to retrieve data from the ArrayList "NamedSpace" but to add data to the "Connection" it doesn't work.

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

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". 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!

 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?

new NamedShape( con.setNameShape1(n,s)

Can you try initializing NameShape object and try to pass it to constructur of Connection?

it would be more on the below lines. 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.

If you want to generate all the combinations among your NamedShape objects you can try with this :

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]));
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM