public class Hm_Test {
public static void main(String[] args){
Array_Test at=new Array_Test();
at.method();
at.method1();
System.out.println("索引为"+at.method2());
}
}
class Array_Test{
private int a[]=null;
private Scanner sc=new Scanner(System.in);
public Array_Test(){
int n;
System.out.println("输入数组长度");
n=sc.nextInt();
a=new int[n];
}
public void method(){
for(int i=0;i<a.length;i++){
System.out.println("输入第"+(i+1)+"个数");
a[i]=sc.nextInt();
}
}
public void method1(){
System.out.print("遍历数组[");
for(int i=0;i<a.length;i++){
if(i!=a.length-1)
System.out.print(a[i]+",");
else
System.out.print(a[i]);
}
System.out.print("]");
}
public int method2(){
int i;
System.out.println("输入你要查找的数:");
i=sc.nextInt();
for(int j=0;j<a.length;j++)
if(i==a[j])
return j;
return -1;
}
} |