介绍
在 java 中,我们常用的查找有两种:
-
顺序查找 SeqSearch.java
-
二分查找(后续补充)
顺序查找
import java.util.Scanner; public class Test { public static void main(String[] args) { String names[] = {"刘德华","郭富城","黎明","张学友"}; Scanner input = new Scanner(System.in); System.out.println("请输入名字:"); String myName = input.next(); int index = -1; for (int i = 0 ; i < names.length ; i++){ if (myName.equals(names[i])){ System.out.println("恭喜你找到" + myName); System.out.println("下标为" + i); index = i; break; } } if (index == -1){ System.out.println("没有找到名字!"); } } }