简体   繁体   English

如何将数组列表的数据从一个类迁移到另一个类

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

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 . 所以我有一个ERDBUILDER.java类是一个绘图面板,它允许我绘制存储在arraylist Connection形状。 I would liked to access this arraylist from another class SQL.java and create my sql statement based on the arraylist. 我想从另一个类SQL.java访问此arraylist并基于该arraylist创建我的sql语句。 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(); 我试图放new SQL(); in the main class but it's opening another ERDBUILDER.java class and that not what i want it to do. 在主类中,但它正在打开另一个ERDBUILDER.java类,而这并不是我想要的。 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"); } } } 

Create an instance of the class where you want to create the SQL statement and pass the Connection ArrayList to that class. 创建要在其中创建SQL语句的类的实例,然后将Connection ArrayList传递给该类。

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. 您的ERDBuilder构造函数可以使用List对象,并使用它来构建SQL语句。 Or you can even do this in a method of ERDBuilder. 或者甚至可以使用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. 如果要从main进行操作,则必须将List声明为Class级别变量,这与使用ERDBuilder的方法相同。

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

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