问题
So i have my an ERDBUILDER.java
class is a drawing panel which allows me to draw shapes that are stored inside an arraylist Connection
. I would liked to access this arraylist from another class SQL.java and create my sql statement based on the arraylist. I've tried the codes that follows but i don't know how the main class should be. I've tried to put new SQL();
in the main class but it's opening another ERDBUILDER.java
class and that not what i want it to do.
So how can i run this, how the main class should be?
EDITED Anyone can help?
package project;
import java.awt.Shape;
import java.util.ArrayList;
import project.ERDBUILDER.DrawingBoard.Attribute;
import project.ERDBUILDER.DrawingBoard.Connection;
import project.ERDBUILDER.DrawingBoard.Connection2;
import project.ERDBUILDER.DrawingBoard.NamedShape;
public class SQL {
public static void main(String args[]){
ArrayList<Connection> con = new ArrayList<>();
for (int a = 0; a < con.size(); a++) {
NamedShape f = con.get(a).getNamedShape1();
Attribute g = con.get(a).getNamedShape2();
String i = f.getName();
String j = g.getName();
Shape y = f.getShape();
Shape y1 = g.getShape();
System.out.println(i + " AND " + j + " are linked");
}
}
}
回答1:
Create an instance of the class where you want to create the SQL statement and pass the Connection ArrayList to that class.
List<Connection> con = new ArrayList<>();
ERDBuilder x = new ERDBuilder(con);
Your ERDBuilder constructor can take a List object and use it to build a SQL statement. Or you can even do this in a method of ERDBuilder. That is your choice.
ERDBuilder x = new ERDBuilder();
List<Connection> con = new ArrayList<>();
x.buildSql(con);
If you want to do it from main then you have to declare the List as a Class level variable, the same way you have done with the ERDBuilder.
来源:https://stackoverflow.com/questions/29204107/how-to-migrate-the-data-of-my-arraylist-from-one-class-to-another-class