Java 如何在ArrayList中搜索?

Java 如何在ArrayList中搜索?,java,Java,我必须按姓名、姓氏或电话号码查找联系人。 我该怎么做? 我想我不能在这种情况下使用Java8流。。。 还有一件事我必须按“2”两次才能看到所有联系人列表,但我认为if实现似乎是正确的 守则: import java.util.ArrayList; import java.util.Scanner; public class Test1 { public static void main(String args[]) { Scanner in = new Scanner( System.

我必须按姓名、姓氏或电话号码查找联系人。 我该怎么做? 我想我不能在这种情况下使用Java8流。。。 还有一件事我必须按“2”两次才能看到所有联系人列表,但我认为if实现似乎是正确的

守则:

import java.util.ArrayList;
import java.util.Scanner;

public class Test1 {
public static void main(String args[]) {
    Scanner in = new Scanner( System.in );
    ArrayList<Contatto> cont = new ArrayList<Contatto>();
    int a;
    try{
        while (true){
            System.out.println("Per inserire nuovo contatto premere 1");
            System.out.println("Per visionare l'elenco dei contatti premere 2");
            System.out.println("Per cercare un contatto premere 3");
            a= in.nextInt();
            //if you press1 you add new contact
            if (a == 1){
                cont.add(new Contatto()); 
            }
            //if you press2 you'll see all the contact list.
            else if (a == 2){
                for (Contatto tmp : cont){
                    String tm = tmp.dammiDettagli(tmp);
                    System.out.println(tm);
                }
            }
            //if you press 3 you'll be able to search a contact by name or
            //surname or telephone number
            else if (a == 3){
                System.out.println("Cerca contatto:");
                }
            else {
                System.out.println("Inserimento dati errato");
                }
            }

    }finally {
        in.close();
    }
}       
}

您可以为正在执行的每种类型的搜索添加一个方法,并在选项“3”中调用该方法。例如,对于按电话号码进行搜索,该方法简单而简单

private static Contatto SearchContattoNumber( ArrayList<Contatto> cont, int number ) {
  for (Contatto tmp : cont){
    if (tmp.getNum_Tel() == number) return tmp;
  }
  return null;
}
private static Contatto searchcontattonnumber(ArrayList cont,int number){
对于(Contatto tmp:cont){
if(tmp.getNum_Tel()==number)返回tmp;
}
返回null;
}

您可以为每个搜索选项创建一个这样的方法,然后通过询问用户来决定要使用哪个搜索,就像您询问要执行的操作一样。这可以在Java 8中完成,如下所示:

Optional<Contatto> result = cont.stream().filter(c ->
    (nome == null || nome.equalsIgnoreCase(c.getNome()))
        && (cognome == null || cognome.equalsIgnoreCase(c.getCognome()))
        && (num_Tel == null || num_Tel.equals(c.getNum_Tel()))
).findFirst();

每次使用
nextInt()。所以不要在
if
语句中这样做。对它执行一次,并将结果存储为
int choice=in.nextInt()
,然后在
if(choice==1){..}或if(choice==2){..}
等条件下使用存储的值。您也可以在此处使用
switch/case
。如果存储结果,则无法更改该值。,。我使用while(true){..}是因为我想重新做这个选择。你可以,只要在你的循环中读一次。这将允许您在每次迭代中更新该值。谢谢!它工作得很好!;)
Optional<Contatto> result = cont.stream().filter(c ->
    (nome == null || nome.equalsIgnoreCase(c.getNome()))
        && (cognome == null || cognome.equalsIgnoreCase(c.getCognome()))
        && (num_Tel == null || num_Tel.equals(c.getNum_Tel()))
).findFirst();
Optional<Contatto> result = cont.stream().filter(c ->
    nome.map(s -> s.equalsIgnoreCase(c.getNome())).orElse(true)
        && cognome.map(s -> s.equalsIgnoreCase(c.getCognome())).orElse(true)
        && num_Tel.map(s -> s.equals(c.getNum_Tel())).orElse(true)
).findFirst();