How to migrate the data of my arraylist from one class to another class

荒凉一梦 提交于 2019-12-12 06:12:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!