有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

作为任务的一部分,我试图从另一个类访问克隆的数组列表,以便利用它。但是当尝试这样做时,我得到了以下错误“非静态方法getConnections()不能从静态上下文中引用”

这是我用来访问克隆阵列的代码。这是在制定从一个目的地飞往另一个目的地的最佳方式的背景下进行的

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

这个类的代码就是这样开始的。它一开始是静态的,但据我所知,它只会影响第一个方法。我如何告诉java这个方法不应该被认为是静态的,这样我就可以从非静态类访问克隆列表了

import java.util.*;


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

我漏掉了很多代码,因为我认为我可能不适合把每一件事都写出来。但是如果你需要更多来获得更清晰的图片,我会很高兴地添加更多的代码

这是另一个类的代码,该类包含我试图访问的克隆数组

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

共 (1) 个答案

  1. # 1 楼答案

    City实际上不提供静态getConnections()方法,因为这没有意义。连接取决于实际的City实例,如果您有权访问一个实例,则可以对其调用getConnections(),甚至可以从静态方法调用

    这是在getConnections()中克隆的数组列表上的注释:

    // Which cities are connected to this one

    请注意,这意味着如果不指定thiscity(获取连接的城市),就无法获取连接,因此只能在City类上调用该方法

    对该方法本身的评论:

    Purpose: To retrieve a list of cities you can reach from this one.


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

    你的逻辑似乎很奇怪。我假设你想实际计算fromto城市之间的路线,而你现在没有这样做。然而,解决这个问题对你来说是一项锻炼

    然后,您可以在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);
    }