使用Get和Set时是否可以应用搜索功能?

发布于 2024-11-02 05:49:00 字数 1248 浏览 0 评论 0原文

抱歉,我是 Java 新手,对此问题可能有一个非常简单的答案。

目前,我正在单独的 JOptionPane 中打印所有结果。
我想设置一个搜索功能,以允许我根据给定的条件显示某些结果。

到目前为止,这是我的代码:

public class Main {

    public static void main(String[] args) {

        //Create new Person objects
        Address p1 = new Address("27","Abbey View","Hexham","NE46 1EQ");
        Address p2 = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
        Address p3 = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");

        Details c1 = new Details();
        Details c2 = new Details();
        Details c3 = new Details();

        //Send some messages to the  objects
        c1.setBeds("3 ");
        c2.setBeds("6");
        c3.setBeds("4");

        c1.setPrice("175,000");
        c2.setPrice("300,00");
        c3.setPrice("250,000");

        c1.setType("Terraced");
        c2.setType("Bungalow");
        c3.setType("Detached");

        //Set up the association
        p1.ownsDetails(c1);
        p2.ownsDetails(c2);
        p3.ownsDetails(c3);

        //Print result
        p1.printDetails();
        p2.printDetails();
        p3.printDetails();

        //Finally quit
        System.exit(0);
    }
}

任何帮助将不胜感激,谢谢。

Sorry, I'm new to Java and there's probably a very simple answer to this.

At the moment, I'm printing all the results out in individual JOptionPanes.
I would like to set up a search function to allow me to show certain results based on a given criteria.

Here's my code so far:

public class Main {

    public static void main(String[] args) {

        //Create new Person objects
        Address p1 = new Address("27","Abbey View","Hexham","NE46 1EQ");
        Address p2 = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
        Address p3 = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");

        Details c1 = new Details();
        Details c2 = new Details();
        Details c3 = new Details();

        //Send some messages to the  objects
        c1.setBeds("3 ");
        c2.setBeds("6");
        c3.setBeds("4");

        c1.setPrice("175,000");
        c2.setPrice("300,00");
        c3.setPrice("250,000");

        c1.setType("Terraced");
        c2.setType("Bungalow");
        c3.setType("Detached");

        //Set up the association
        p1.ownsDetails(c1);
        p2.ownsDetails(c2);
        p3.ownsDetails(c3);

        //Print result
        p1.printDetails();
        p2.printDetails();
        p3.printDetails();

        //Finally quit
        System.exit(0);
    }
}

Any help would be appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2024-11-09 05:49:00

您可能会发现拥有一个数组比使用三个单独的变量更有帮助:

Address p[] = new Address[3];
p[0] = new Address("27","Abbey View","Hexham","NE46 1EQ");
p[1] = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
p[2] = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");

现在可以编写一个循环来检查每个变量:

for(int i = 0; i < p.length; i++) {
    if (p[i].getSomething().equals(thingToSearch)) {
        // ... it's a match!
    }
}

Instead of having three separate variables, you may find it helpful to have an array:

Address p[] = new Address[3];
p[0] = new Address("27","Abbey View","Hexham","NE46 1EQ");
p[1] = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
p[2] = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");

Now it's possible to write a loop to examine each one:

for(int i = 0; i < p.length; i++) {
    if (p[i].getSomething().equals(thingToSearch)) {
        // ... it's a match!
    }
}
温柔少女心 2024-11-09 05:49:00

是的,您可以创建一个自定义 JDialogJFrame,它具有多个输入字段和由于搜索而显示的多个结果。

理想情况下,您的对象需要位于列表或数组中才能进行搜索,
这样你就可以遍历所有这些并找到匹配项。

Yes, you can create a custom JDialog or JFrame that has multiple input fields and multiple results that appear due to searching.

Ideally, your objects would need to be in a list or array to do the searching,
so that you can loop over all of them and find matches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文