简体   繁体   中英

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".

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??

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. 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.

This is the comment on the array list that is cloned in 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.

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:

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. I assume you want to actually calculate the route between the from and the to city, which you are not doing right now. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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