package ch08;
import java.util.Scanner;
/**
* Created by liwenj on 2017/7/20.
*/
public class Person {
String[] names = new String[30];
//把为null的元素添加内容
void addName(String name) {
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
names[i] = name;
break;
}
}
System.out.println("插入名字是" + name);
}
//把不是null的元素(即是插入的元素)显示出来
void showName() {
for (int i = 0; i < names.length; i++) {
if (names[i] != null) {
System.out.println("得到的名字是" + names[i]);
}
}
}
//判断在数组某个区间内某个元素存不存在
boolean find(int begain, int end, String name) {
boolean f=false;
for(int j=0;j<end;j++){
if(names[j].equals(name)){
f=true;
break;
}
}
return f;
}
//找到数组中某个区间内某个元素的位置,返回该元素索引
int find2(int begain, int end, String name) {
int i=-1;
for(int j=0;j<=end;j++){
if(names[j].equals(name)){
i=j;
break;
}
}
return i;
}
}
class test {
public static void main(String[] args) {
boolean s;
int r;
Person person = new Person();
//从键盘输入五个元素
for(int i=0;i<5;i++){
System.out.println("请输入姓名:");
Scanner input=new Scanner(System.in);
String name=input.next();
person.addName(name);
}
person.showName();
s=person.find(0,3,"ccc");
System.out.println(s);
r=person.find2(0,3,"fff");
System.out.println(r);
}
}