简体   繁体   English

如何访问非静态克隆的arrayList以用于Java中的静态方法

[英]How to access non static cloned arrayList for use in static method in Java

As part of an assignment Im trying to access a cloned array list from another class so I can utilize it. 作为一项任务的一部分,我试图从另一个类访问克隆的数组列表,以便我可以利用它。 But when attempting to do so I get the following error "non-static method getConnections() cannot be refrenced from a static context". 但是当试图这样做时,我得到以下错误“非静态方法getConnections()不能从静态上下文中引用”。

This is the code I'm using to access the cloned array. 这是我用来访问克隆数组的代码。 It is in the context of working out the best way to take flights from one destination to another. 在制定从一个目的地到另一个目的地的航班的最佳方式的背景下。

public boolean determineRoute(City from, City to, ArrayList<City> flightRoute)
{
    ArrayList<City> Connections = new ArrayList<City>();
    Connections = City.getConnections(); 
    return true;
}

And this is how the code for that class begins. 这就是该类的代码开始的方式。 It does start as static but as far as i can see it should only affect the first method how can I tell java that this method should not be considered static so I can access the cloned list from the non static class?? 它确实以静态方式启动,但据我所知它应该只影响第一种方法如何告诉java这个方法不应该被认为是静态的,所以我可以从非静态类访问克隆列表?

import java.util.*;


public class Lab9_Ex2_Main
{
    //////// START-UP /////////
    public static void main(String[] args)
    {
        new Lab9_Ex2_Main();
    }

I have left out a lot of the code as I think it may not be right from me to put every thing up. 我遗漏了很多代码,因为我认为将所有内容都放在一边可能不正确。 But should you need more to get a clearer picture I will happily add more of the code. 但是,如果您需要更多以获得更清晰的图片,我将很乐意添加更多代码。

This is the code from another class which contains a cloned array which im attempting to access. 这是来自另一个类的代码,其中包含我试图访问的克隆数组。

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

City actually doesn't provide a static getConnections() method, since that doesn't make sense. City实际上不提供静态getConnections()方法,因为这没有意义。 The connections depend on an actual City instance and if you have access to one you can call getConnections() on it, even from a static method. 连接取决于实际的City实例,如果您有权访问,则可以在其上调用getConnections() ,即使是静态方法也是如此。

This is the comment on the array list that is cloned in getConnections() : 这是在getConnections()克隆的数组列表的注释:

// Which cities are connected to this one //哪个城市与城市相连

Note that this means you just can't get the connections without specifying this city (the one you get the connections for) and thus just can't call that method on the City class only. 请注意,这意味着您无法在未指定城市(您获得连接的城市)的情况下获取连接,因此无法仅在City类上调用该方法。

Comment on the method itself: 评论方法本身:

Purpose: To retrieve a list of cities you can reach from this one . 目的:获取城市的列表,你可以从这个一个到达。


Assuming your determineRoute(...) method might be static, it could look like this: 假设您的determineRoute(...)方法可能是静态的,它可能如下所示:

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

Your determineRoute(...) logic seems quite odd. 你的determineRoute(...)逻辑似乎很奇怪。 I assume you want to actually calculate the route between the from and the to city, which you are not doing right now. 我假设你想实际计算之间的路由fromto城市,你是不是做现在。 Fixing that, however, is an exercise for you. 但是,确定这是一项练习。

You could then call that method in your main method (which has to be static) like this: 然后,您可以在main方法中调用该方法( 必须是静态的),如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM