简体   繁体   English

显示搜索结果 JAVA ArrayLists

[英]displaying search results JAVA ArrayLists

I am supposed to use the method exactly(all my methods have to return void)我应该完全使用该方法(我所有的方法都必须返回 void)

public void searchByPriceRange(double minPrice, double maxPrice)

The method will search the ArrayList<Property> for properties where the asking price is between the minPrice and maxPrice and add each match to a local ArrayList<Property> that will then be passed to another method that has to be该方法将在ArrayList<Property>中搜索要价介于 minPrice 和 maxPrice 之间的属性,并将每个匹配项添加到本地ArrayList<Property> ,然后将其传递给另一个必须

displaySearchResults(ArrayList<Property> searchResults)

How would I create a local ArrayList<Property> that is accessible outside the scope of the first method?(if thats what I am supposed to do)我将如何创建一个本地ArrayList<Property>可以在第一种方法的 scope 之外访问?(如果那是我应该做的)

my instance variable and initialization of ArrayList我的实例变量和 ArrayList 的初始化

 private ArrayList<Property> properties = new ArrayList<Property>();

my method in AgentListing class我在 AgentListing class 中的方法

public void searchByPriceRange(double minPrice, double maxPrice){

    ArrayList<Property> searchResults;
    searchResults = new ArrayList<Property>();
    for (Property property : properties){
        if (property.getAskingPrice() > minPrice &&
                property.getAskingPrice() < maxPrice){


            searchResults.add(property);
           
        }
    }
}

my display method AgentListing class我的显示方法 AgentListing class

public void displaySearchResults(ArrayList<Property> searchResults){


    for(Property result: searchResults) {

        System.out.println(result);
    }

}

my driver class that I am trying to access the local arraylist with我的驱动程序 class 我正在尝试访问本地 arraylist

AgentListings CharlesListings = new AgentListings();

        Property one = new Property("1ListingNumber", "777 Imaginary Drive, Nowhere, British Columbia", 1.00, "Single Family", "House",
            "1.1 Acre", 2007);

    one.setNumberOfBedrooms(1);
    one.setNumberOfBathrooms(1);


    Property two = new Property("2ListingNumber", "777 Imaginary Drive, Nowhere, British Columbia", 2.00, "Single Family", "House",
            "2.2 Acre", 2007);

    two.setNumberOfBedrooms(2);
    two.setNumberOfBathrooms(3);

CharlesListings.addProperty(one);
CharlesListings.addProperty(two);

CharlesListings.searchByPriceRange(1.0, 2.0);

CharlesListings.displaySearchResults(searchResults); //searchResults is not reachable

I added the full instructions to my assignment if I am not clear 如果我不清楚,我会将完整的说明添加到我的作业中

How would I create a local ArrayList that is accessible outside the scope of the first method?(if thats what I am supposed to do)我将如何创建一个本地 ArrayList 可以在第一种方法的 scope 之外访问?(如果那是我应该做的)

here:这里:

public void searchByPriceRange(double minPrice, double maxPrice)

return a list (searchResults) instead of void...返回一个列表(searchResults)而不是 void...

you can define a temp variable, or just queue the returned value as param to the display method您可以定义一个临时变量,或者只是将返回的值作为参数排队到显示方法

ArrayList<Property> x = searchByPriceRange(min, max);
displaySearchResults(x);

or或者

displaySearchResults(searchByPriceRange(min, max));

searchResult is a local variable, so it's not visible outside the method it is declared in. A possible solution would be to change the signature of the searchByPriceRange method from: searchResult是一个局部变量,因此它在声明它的方法之外是不可见的。一个可能的解决方案是将searchByPriceRange方法的签名从:

public void searchByPriceRange(double minPrice, double maxPrice)

to

public List<Property> searchByPriceRange(double minPrice, double maxPrice) . public List<Property> searchByPriceRange(double minPrice, double maxPrice)

Now you can return the searchResults list, so it can be stored in a variable or passed to another method.现在您可以返回searchResults列表,因此可以将其存储在变量中或传递给另一个方法。 Since you only need to pass it to the displaySearchResults method, you could write:由于您只需要将它传递给displaySearchResults方法,您可以编写:

CharlesListings.displaySearchResults(CharlesListings.searchByPriceRange(1.0, 2.0));

Modify searchByPrice(...) method like this:像这样修改searchByPrice(...)方法:

public ArrayList<Property> searchByPriceRange(double minPrice, double maxPrice){

    ArrayList<Property> searchResults;
    searchResults = new ArrayList<Property>();
    for (Property property : properties){
        if (property.getAskingPrice() > minPrice &&
                property.getAskingPrice() < maxPrice){

              searchResults.add(property);
        }
    }
    return searchResults;
}

Now, when it returns searchResults array, you can use the returned value as a parameter for displaySearchResults(searchResults) method.现在,当它返回 searchResults 数组时,您可以将返回的值用作displaySearchResults(searchResults)方法的参数。 Like this:像这样:

ArrayList<Property> searchResultsList = CharlesListings.searchByPriceRange(1.0, 2.0);
CharlesListings.displaySearchResults(searchResultsList);

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

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