如何访问非静态克隆arrayList以用于Java中的静态方法
作为任务的一部分,我试图从另一个类访问克隆的数组列表,以便可以利用它。但是当尝试这样做时,我得到以下错误“非静态方法getConnections()不能从静态上下文中引用” 这是我用来访问克隆阵列的代码。这是在制定从一个目的地飞往另一个目的地的最佳方式的背景下进行的如何访问非静态克隆arrayList以用于Java中的静态方法,java,static,clone,Java,Static,Clone,作为任务的一部分,我试图从另一个类访问克隆的数组列表,以便可以利用它。但是当尝试这样做时,我得到以下错误“非静态方法getConnections()不能从静态上下文中引用” 这是我用来访问克隆阵列的代码。这是在制定从一个目的地飞往另一个目的地的最佳方式的背景下进行的 public boolean determineRoute(City from, City to, ArrayList<City> flightRoute) { ArrayList<City> Con
public boolean determineRoute(City from, City to, ArrayList<City> flightRoute)
{
ArrayList<City> Connections = new ArrayList<City>();
Connections = City.getConnections();
return true;
}
我遗漏了很多代码,因为我认为我可能不应该把每一件事都写出来。但是,如果您需要更多来获得更清晰的图片,我将很高兴地添加更多的代码
这是另一个类的代码,该类包含我试图访问的克隆数组
import java.util.*;
// Class: City
// Purpose: To represent a place in the world that you can fly from/to.
public class City
{
private String name; // The name of the City
private ArrayList<City> connectsWith; // Which cities are connected to this one
public City(String cityName)
{
name = cityName;
connectsWith = new ArrayList<City>();
}
// Method: addConnection
// Purpose: To note that you can catch a flight to the destination, from this city
// Passed:
// destination - The City which you can fly to.
public void addConnection(City destination)
{
if (destination != null && destination != this)
connectsWith.add(destination);
}
// Method: getConnections
// Purpose: To retrieve a list of cities you can reach from this one.
// Note: You are given a clone, (to avoid a privacy leak), and can manipulate it however
// you like. E.g. you could delete elements.
public ArrayList<City> getConnections()
{
return (ArrayList<City>) connectsWith.clone();
}
public String getName()
{
return name;
}
public String toString()
{
return name;
}
}
import java.util.*;
//类别:城市
//目的:代表世界上一个你可以飞来飞去的地方。
公营城市
{
私有字符串名称;//城市名称
private ArrayList connectsWith;//哪些城市连接到此城市
公共城市(字符串cityName)
{
名称=城市名称;
connectsWith=newarraylist();
}
//方法:addConnection
//目的:注意你可以从这个城市搭乘到目的地的航班
//通过:
//目的地-你可以飞到的城市。
公共连接(城市目的地)
{
if(destination!=null&&destination!=this)
与add(目的地)连接;
}
//方法:getConnections
//目的:检索可从此列表中访问的城市列表。
//注意:您获得了一个克隆(以避免隐私泄露),但可以对其进行操作
//您喜欢。例如,您可以删除元素。
公共ArrayList getConnections()
{
返回(ArrayList)connectsWith.clone();
}
公共字符串getName()
{
返回名称;
}
公共字符串toString()
{
返回名称;
}
}
City
实际上不提供静态的getConnections()
方法,因为这没有意义。连接取决于一个实际的City
实例,如果您有权访问一个实例,则可以对其调用getConnections()
,甚至可以通过静态方法调用
这是在getConnections()
中克隆的数组列表上的注释:
//哪些城市与这个城市相连
请注意,这意味着如果不指定此城市(获取连接的城市),就无法获取连接,因此只能在city
类上调用该方法
对方法本身的评论:
目的:检索可从此列表中访问的城市列表
假设您的
determiniteroute(…)
方法可能是静态的,它可能如下所示:
public static boolean determineRoute(City from, City to, ArrayList<City> flightRoute)
{
ArrayList<City> connections = new ArrayList<City>();
connections = to.getConnections(); //or from.getConnections(); what ever makes sense
//connections is not used, so I assume you want to put them into flightRoute
flightRoute.addAll(connections);
return true;
}
public static void main(String... args) {
City berlin = new City("Berlin");
City beijing = new City("Beijing");
//fill the connections here
ArrayList<City> route = new ArrayList<City>();
boolean success = determineRoute(berlin, beijing, route);
}
如果你学会正确地格式化你的问题,那么文本就不会成为代码,你就不需要中间的胡言乱语了。请学习如何使用该系统,而不是模仿它…请尝试并学习如何在不添加“胡言乱语”文本的情况下格式化您的帖子:为你编辑的。很抱歉打扰你。我将如何格式化文本,使其不作为代码出现?感谢您花时间回答。那么,为什么我在尝试访问克隆的arraylist时会收到错误消息呢?哎呀,我刚才发表评论时还没有看到你的全部帖子。是的,谢谢,你已经解释过了,这很有道理。当然我不能访问城市,因为它还没有被实例化!!。啊哈,谢谢。是的,这是一个基于递归的任务,航班只从一个城市飞往另一个城市,我们需要计算出需要经过哪些国家才能获得他们的航班。
public static void main(String... args) {
City berlin = new City("Berlin");
City beijing = new City("Beijing");
//fill the connections here
ArrayList<City> route = new ArrayList<City>();
boolean success = determineRoute(berlin, beijing, route);
}