![](/img/trans.png)
[英]Java, how to add one object to an ArrayList and decrease one from another
[英]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“連接”
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))));
}
你能幫我嗎?
EDITED
因此,我下面的代碼應允許用戶繪制形狀,並且在繪制形狀時,用戶輸入形狀的名稱,該名稱保存在數組“ 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對象之前設置它?
新的NamedShape( con.setNameShape1(n,s)
您可以嘗試初始化NameShape對象,然后嘗試將其傳遞給Connection的構造方法嗎?
以下幾行會更多。 con.add(new Connection(new NamedShape(“ abc”,shapObj),new NamedShape(“ xyz”,shapObj)));
或查看是否可以在Connection對象中對其進行初始化,並且其中一種方法返回給定的對象。
如果要在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.