import java.util.Scanner;
public class Practice19_4 {
public static <E extends Comparable<E>> int linearSearch(E[] list,E key) {
int i;
for( i = 0 ; i<list.length ; i++)
{
if(list[i].compareTo(key)==0)
{
break;
}
}
if(i<list.length)
return i;
else
return -1;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入数量:");
int num = in.nextInt();
String[] list = new String[num];
System.out.println("请输入要插入的元素");
for(int i = 0 ; i < list.length ; i ++)
{
list[i] = in.next();
}
System.out.println("请输入要查询的元素");
String key = in.next();
if(linearSearch(list, key)>=0) {
System.out.println("字符:"+list[linearSearch(list, key)]+"的位置:"+linearSearch(list, key));
}
else
{
System.out.println("没有找到");
}
in.close();
}
}
int i;
for( i = 0 ; i<list.length ; i++)
{
if(list[i].compareTo(key)==0)
{
break;
}
}
if(i<list.length)
return i;
else
return -1;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入数量:");
int num = in.nextInt();
String[] list = new String[num];
System.out.println("请输入要插入的元素");
for(int i = 0 ; i < list.length ; i ++)
{
list[i] = in.next();
}
System.out.println("请输入要查询的元素");
String key = in.next();
if(linearSearch(list, key)>=0) {
System.out.println("字符:"+list[linearSearch(list, key)]+"的位置:"+linearSearch(list, key));
}
else
{
System.out.println("没有找到");
}
in.close();
}
}