【问题标题】:How to store search results and display data JAVAJAVA如何存储搜索结果和显示数据
【发布时间】:2020-07-07 06:57:09
【问题描述】:

我被卡住了

我有两种需要的方法

public void searchByPriceRange(double minPrice, double maxPrice)

public void displaySearchResults(ArrayList<Property> searchResults)

两者都需要返回 void。

我被允许拥有的唯一实例变量是(不确定是哪一个)

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

private ArrayList<Property> properties;

我需要搜索一个名为 properties 的数组列表,并将适合价格范围的属性存储在 LOCAL ArrayList 中,然后将其传递给 displaySearchResults(ArrayList searchResults) 进行显示。

如何创建本地数组列表?调用 displaySearchReults 方法时如何访问本地数组列表中的数据?如何调用显示搜索结果方法?

现在,我认为我的 searchByPriceRange 方法将数据存储在名为 searchResults 的局部变量中。但是我不明白如何调用displaySearchResults方法并在searchByPriceRange方法创建的本地arraylist中显示数据。

我在 agentListings 类中的完整方法

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

public void displaySearchResults(ArrayList<Property> searchResults){


    for(Property result: searchResults) {

        System.out.println(result.getListingNumber());
    }
}

我的司机课

AgentListings CharlesListings = new AgentListings();

CharlesListings.displaySearchResults(CharlesListings.searchByPriceRange(1, 2));

这条线不起作用。如果方法参数必须是,不确定在参数中放入什么

public void displaySearchResults(ArrayList<Property> searchResults)

【问题讨论】:

  • 您确定searchByPriceRange 不会返回ArrayList?另外,为什么要为每个结果打印ArrayList 的大小?它永远是一样的;)
  • 或者displaySearchResults 方法实际上不应该打印任何东西,而是用搜索结果填充给定参数?这个名字会有点误导,但它可以避免将对象自己的变量作为参数传递,并且在这方面更有意义。
  • @Mark 说明显示了一个带有 void 的方法,但我不确定说明是否错误。感谢您指出我的错误,我已修复它。
  • 我试图理解您的第二条评论在说什么。生病需要一些时间。谢谢
  • 这些是我正在完成的作业的说明

标签: java arrays list


【解决方案1】:

我刚刚找到了我们在说明中忽略的部分。

public void searchByPriceRange(double minPrice, double maxPrice)

将在 ArrayList 中搜索请求的属性 price 介于 minPrice 和 maxPrice 之间,并将每个匹配项添加到 然后将传递给的本地 ArrayList displaySearchResults(ArrayList searchResults) 用于显示

你确实在你的问题中提到了它,但是你写的其他所有东西都让我想错了方向;)

这意味着:

public class AgentListings {
    private ArrayList<Property> properties = new ArrayList<Property>();     
    
    public void searchByPriceRange(double minPrice, double maxPrice) {
        ArrayList<Property> searchResults = new ArrayList<Property>(); // <- This is your local ArrayList
        
        for (Property property : properties){
            if (property.getAskingPrice() >= minPrice && property.getAskingPrice() <= maxPrice) {
                searchResults.add(property);
            }
        }

        displaySearchResults(searchResults); // <- And here we go calling the method with the filtered list
    }

    public void displaySearchResults(ArrayList<Property> searchResults){
        System.out.println(searchResults.size());
        for(Property result: searchResults) { 
            System.out.println(result);
        }
    }
}

这里的重要部分是我们不要更改我们的类记住的属性。我们新建一个本地ArrayList 并立即显示。

【讨论】:

  • 要去尝试理解这一点,看起来这会给我带来微笑。谢谢
【解决方案2】:

不是在函数内部声明arrayList,而是在类中声明它

class{
    ArrayList<Property> searchResults = new ArrayList<Property>();

public void searchByPriceRange(double minPrice, double maxPrice){

   
    for (Property property : properties){
        if (property.getAskingPrice() > minPrice &&property.getAskingPrice() < maxPrice){
            this.searchResults.add(property); \\You can remove "this." as it may work without that
        }
    }
}

public void displaySearchResults(ArrayList<Property> searchResults){


    for(Property result: this.searchResults) { \\You can remove "this." as it may work without that

        System.out.println(result);
        System.out.println(searchResults.size());
    }
}
}

【讨论】:

  • 驾驶课怎么写?也是在函数 local 之外声明 ArrayList 吗?
  • 不错的答案,但是它在某种程度上使displaySearchResults 的参数无效,因为您不再需要(或想要)它。对于一个类来说,让用户将自己的ArrayList 交给它的一个方法是非常奇怪的行为......
  • @GrantLee 允许的实例变量前面的private 表明它应该是一个类变量。如果您在方法中声明 ArrayList,则添加 private 几乎没有意义。
  • 就像 Siddharth 在这里所做的一样(加上前面的私人可能;))。话虽如此,我仍然认为这不是给你这个任务的人所想的,只是因为参数变得如此无用......除了我在你的问题中提到的 cmets 之外,我还不能想到其他选择...
  • 来自@GrantLee 在他的 cmets 中链接到的说明:This new class will have one field, an ArrayList&lt;Property&gt; properties. 所以是的,Siddarth 正是他们想要的。我会从循环中删除this.(就像他的评论所暗示的那样),只是为了安全起见(说明非常具体地说明了你应该在此处显示的参数)。这看起来有些奇怪,但看起来这实际上是你应该做的。
猜你喜欢
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
相关资源
最近更新 更多