如何将我的数组列表的数据从一个类迁移到另一个类 [英] How to migrate the data of my arraylist from one class to another class

查看:36
本文介绍了如何将我的数组列表的数据从一个类迁移到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 ERDBUILDER.java 类是一个绘图面板,它允许我绘制存储在数组列表 Connection 中的形状.我想从另一个类 SQL.java 访问这个数组列表,并基于数组列表创建我的 sql 语句.我已经尝试了后面的代码,但我不知道主类应该如何.我试图将 new SQL(); 放在主类中,但它打开了另一个 ERDBUILDER.java 类,这不是我想要的.那么我该如何运行它,主类应该如何?

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?

已编辑有人可以帮忙吗?

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

推荐答案

创建要在其中创建 SQL 语句的类的实例,并将 Connection ArrayList 传递给该类.

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

您的 ERDBuilder 构造函数可以采用 List 对象并使用它来构建 SQL 语句.或者您甚至可以在 ERDBuilder 的方法中执行此操作.那是你的选择.

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

如果您想从 main 中执行此操作,则必须将 List 声明为类级别变量,就像您对 ERDBuilder 所做的一样.

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.

这篇关于如何将我的数组列表的数据从一个类迁移到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆