Java 使用Get和Set时是否可以应用搜索功能?
对不起,我是Java新手,对此可能有一个非常简单的答案 目前,我正在将所有结果打印在单个Java 使用Get和Set时是否可以应用搜索功能?,java,forms,search,setter,getter,Java,Forms,Search,Setter,Getter,对不起,我是Java新手,对此可能有一个非常简单的答案 目前,我正在将所有结果打印在单个JOptionPanes中。 我想设置一个搜索功能,以便根据给定的条件显示某些结果 以下是我目前的代码: public class Main { public static void main(String[] args) { //Create new Person objects Address p1 = new Address("27","Abbey View"
JOptionPane
s中。我想设置一个搜索功能,以便根据给定的条件显示某些结果 以下是我目前的代码:
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);
}
}
非常感谢您的帮助。如果没有三个单独的变量,您可能会发现数组非常有用:
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!
}
}
for(int i=0;i
是的,您可以创建一个自定义的JDialog
或JFrame
,它有多个输入字段和多个搜索结果
理想情况下,您的对象需要在列表或数组中进行搜索,这样,您就可以对所有这些内容进行循环并找到匹配项。这更有意义,感谢您的快速响应:)