【问题标题】:How to access an Arraylist of another class in java如何在java中访问另一个类的Arraylist
【发布时间】:2015-03-12 11:34:02
【问题描述】:

我知道这可能是一个基本问题,但我为这个问题得到的代码不适用于我的数组列表。可能有人可以帮我解决这个问题。我想访问Testing1.Java 类的数组列表“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 类允许我绘制形状,经过一些处理后,形状存储到数组列表Connection。然后我有我的类SQL.java,我想通过从ERDBUILDER.java 调用类SQL.java 来使用该数组列表Connection,但我不希望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[]){

       
       
   }
}

【问题讨论】:

  • 请包含这些代码 sn-ps 来自的文件的名称,例如SQL.java 等。
  • 你能把你的Testing1类的源贴出来
  • Testing1 类在哪里
  • 顺便说一下,您将“Connection”和“NamedShape”都称为数组。这些都不是数组。您确实有两个名为 shapes 和 con 的字段,但它们是相关对象的 ArrayList,而不是数组。为避免混淆(对您自己和他人),可能值得纠正这一点。
  • @richzilla 它告诉我我已经超过了字符数。这就是为什么我没有发布Testing.Java。我的数组位于我的 Testing1.Java 中,并且数据被添加到数组“Connection”中所以我只想从 Testing1.Java 访问数组“Connection”并将其用于 Sql.Java

标签: java arrays class arraylist getter


【解决方案1】:

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

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

快速解答

getConnection() 在线路中对您不可用

ArrayList<Connection> con = sql.getConnection()

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

修复

  1. 使drawPanel 成为Testing1 的成员变量,以便您可以从“外部”访问它,或者直接将其设为公共成员,或者通过添加返回它的getDrawingBoard() 方法。例如

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

评论

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

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

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

我建议进行重构 - 将 DrawingBoard 拆分为新的类文件 DrawingBoard.java。和Testing1放在同一个包里,Testing1就可以使用了。

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

【讨论】:

  • @J Richard Snape 感谢您长时间而全面的回复。但是当我运行 .SQL 类时,我不希望 java 打开另一个 Testing1 类。你能告诉我我应该在主课上写什么吗?请看我上面编辑的代码。
  • 嗯,我可以看到你的编辑。这里有几个问题:1.您的问题是“迁移”......现在它与原始问题完全不同,因此您应该真正进行编辑并提出一个新问题,参考这个问题。 2. 我觉得这里有更深的误解——可能是XY problem。我不明白为什么您拥有的代码在 SQL 类的构造函数中。您暗示您可能想从许多不同的课程中访问相同的ERDBUILDER,但目前尚不清楚...
  • 换句话说 - 提出一个新问题,有更多的上下文,参考这个问题。准确解释您要做什么 - SQL 类实际上是做什么用的?为什么不直接调用ERDBUILDER?是否存在更大的问题/代码上下文,您正在尝试将其融入其中?
  • @J Richard Snape 好的,我将在此基础上发布另一个新问题。事实上,我想做的是在 sql.java 中创建我的 SQL 语句,但使用 ERDBUILDER.java 类中的数组列表。
  • 顺便说一句-您对问题的最新编辑意味着它没有多大意义-现在没有人可以看到原始课程等,因此其他阅读它的人将无法理解我的回答。我建议您回滚到以前的版本 - 特别是您现在要求 this related question 报道您的新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多