[英]How to access an Arraylist of another class in java
我知道這可能是一個基本問題,但是我為該問題獲得的代碼不適用於我的arraylist。 可能有人可以幫助我解決這個問題。我想訪問Testing1.Java
類的Testing1.Java
“ Connection”,並將其用於另一個名為SQL.Java
類中。 我的數組“ Connection”是另一個數組“ NamedShape”的對象。
這是我的2個arrayslist:
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
,我想通過從ERDBUILDER.java
調用類SQL.java
來使用該arraylist 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[]){ } }
盡管您在pastebin上發布的代碼可以編譯,但這是一個非常不尋常的模式。
在Testing1
類中有多個嵌套的內部類。 這是有效的Java(即可以編譯),但對於您想要的東西可能不是必需的。
該行無法使用getConnection()
ArrayList<Connection> con = sql.getConnection()
因為您的getConnection()
方法未在Testing1
類中Testing1
,而是在內部類Testing1.DrawingBoard.Connection
的內部類中Testing1.DrawingBoard.Connection
(代碼的第342行)。
讓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... }
將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...
然后,您將能夠在SQL
的構造函數中執行以下操作:
public SQL(){ sql = new Testing1(); ArrayList<Connection> con = sql.getDrawingBoard().getConnection(); }
您正在使用的模式很難遵循和維護。 Java教程中提供了有關何時使用嵌套類的更多信息 。
特別是-您確實不需要DrawingBoard
成為Testing1
的內部類Testing1
可用於許多不同的應用程序,因此它不滿足主要標准
嵌套類使您可以對僅在一個地方使用的類進行邏輯分組
我建議進行重構-將DrawingBoard
分解為新的類文件DrawingBoard.java
。 將其與Testing1
放在同一包中,然后可由Testing1
。
我知道這個答案可能會引發更多問題-如果確實如此-提出一個有關嵌套類而不是在您的特定情況下導入它們並在注釋中鏈接到它的新問題...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.