如何在java中访问另一个类的Arraylist

时间:2015-03-12 11:34:03

标签: java arrays class arraylist getter

我知道这可能是一个基本问题,但我为这个问题得到的代码对我的arraylist不起作用。可能有人可以帮我解决这个问题。我想访问类Testing1.Java的arraylist“Connection”并在另一个名为SQL.Java的类中使用它。我的数组“Connection”是另一个数组“NamedShape”的对象。

这是我的2个数组列表:

ArrayList<NamedShape> shapes = new ArrayList<NamedShape>();
        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 setNamedShape1() {
                this.namedShape1 = namedShape1;
            }

            public void setNamedShape2() {
                this.namedShape2 = namedShape2;
            }
        }

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

我在我的Testing1.Java中放了一个getConnection方法。数据被正确插入到Arraylist连接中(我没有把代码添加到数据中但数据插入正确,这不是问题)

public ArrayList<Connection> getConnection() {
            return con;

        }

这是我的试用SQL.Java,但它无法识别此处的getConnection()方法:

import java.util.ArrayList;

public class SQL {
    private Testing1 sql;
    public SQL(){
        sql = new Testing1();
        ArrayList<Connection> con = sql.getConnection()

    }

   public static void main(String args[]){
       new Test();


   }
}

有人可以帮我解决这个问题吗?

被修改 我已经阅读并按照你所说的,现在我有了这个,但我不知道应该如何。所以我将班级Testing1.java称为ERDBUILDER.java。我的ERDBUILDER类允许我绘制形状,经过一些处理后,形状存储到arraylist Connection。然后我有我的班级SQL.java,我希望通过从Connection调用类SQL.java来使用该arraylist ERDBUILDER.java,但我不希望java打开另一个ERDBUILDER.java }。我已将new SQL();放在主要内容中,但它正在打开另一个ERDBUILDER.java,这不是我想要的。你能建议一下吗?我可能是一个基本问题,但我仍然找不到办法。

package project;
import java.awt.Shape;
import java.util.ArrayList;
import project.ERDBUILDER.DrawingBoard.Attribute;
import project.ERDBUILDER.DrawingBoard.Connection;
import project.ERDBUILDER.DrawingBoard.NamedShape;


public class SQL {
    
    private ERDBUILDER sql;
    public SQL(){
        sql = new ERDBUILDER();
    ArrayList<Connection> con = sql.getDrawingBoard().getConnection();
            
        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();

                                   
        }
    }
    
   public static void main(String args[]){

       
       
   }
}

1 个答案:

答案 0 :(得分:1)

虽然您在pastebin上发布的代码会编译,但这是一种非常不寻常的模式。

您的Testing1课程中有多个nested inner classes。这是有效的Java(即它将编译),但可能不是你想要的。

快速回答

getConnection()

行中无法使用
ArrayList<Connection> con = sql.getConnection()

因为getConnection()方法未在类Testing1中定义,而是内部类Testing1.DrawingBoard.Connection的内部类(代码的第342行)。

修复

  1. 使drawPanel成为Testing1的成员变量,以便您可以直接通过将其作为公共成员,或通过添加一个公共成员来访问&#34;外部&#34;返回它的getDrawingBoard()方法。 e.g。

    public class Testing1 extends JFrame
    {
        private DrawingBoard drawPanel;
    
        public DrawingBoard getDrawingBoard()
        {
            return drawPanel;
        }
    
        //...The rest of your existing code except !!!
        // !!! in the Testing1 constructor at line 238 !!!
        // change          final DrawingBoard drawPanel = new DrawingBoard(); to
    
            drawPanel = new DrawingBoard();
    
        // ... and the rest of your code...
    
    }
    
  2. getConnection()移出Connection - 因此它是DrawingBoard而非Connection的成员。例如(使用现有代码作为基础)

    //...preceding code before line 324 ...
    
    public class DrawingBoard extends JComponent
    {
    
        private static final long serialVersionUID = -4431176095451940075L;
    
        // ArrayList<Shape> shapes = new ArrayList<Shape>();
        ArrayList<Color> shapeStroke = new ArrayList<Color>();
        ArrayList<Integer> count = new ArrayList<Integer>();
        ArrayList<NamedShape> shapes = new ArrayList<NamedShape>();
        ArrayList<Connection> con = new ArrayList<Connection>();
    
        public ArrayList<Connection> getConnection()
        {
           return con;
        }
    
        public class Connection
        {
        // ... rest of your code...
    
  3. 然后,您将能够在SQL的构造函数中执行以下操作:

    public SQL(){
        sql = new Testing1();
        ArrayList<Connection> con = sql.getDrawingBoard().getConnection();
    }
    
  4. 注释

    您使用的模式很难遵循和维护。 Java教程中提供了More information on when to use nested classes

    特别是 - 你真的不需要DrawingBoard成为Testing1的内部类 - DrawingBoard可用于许多不同的应用程序,因此它不能满足主要要求标准

      

    嵌套类使您能够对仅在一个地方使用的类进行逻辑分组

    我建议重构 - 将DrawingBoard分解为新的类文件DrawingBoard.java。将其放在与Testing1相同的包中,然后Testing1可以使用它。

    我知道这个答案可能会引发更多问题 - 如果确实如此 - 提出一个关于嵌套类的新问题,而不是在特定情况下导入它们并在评论中链接到它......