日期:2014-05-20 浏览次数:20765 次
package shiyan2_2;
import java.util.*;
//import java.io.*;
class info
{
public int num;
public String name;
public String sex;
public int pnum;
public String address;
};
public class Tongxun //extends info
{
//info in[];
int size;//记录当前元素个数
public void input(info in[] )
{
Scanner sc=new Scanner(System.in);
//System.out.println(in.length);
for(int i=0;i<in.length;i++)
{
in[i].num=i;
System.out.println("请输入姓名:");
in[i].name=sc.next();
System.out.println("请输入性别:");
in[i].sex=sc.next();
System.out.println("请输入电话:");
in[i].pnum=sc.nextInt();
System.out.println("请输入地址:");
in[i].address=sc.next();
}
size=in.length;
}
public info[] Addarr(info in[])//新增地址
{
System.out.println("请输入您要添加的个数:");
int count=0;
Scanner sc=new Scanner(System.in);
count=sc.nextInt();
info[] newinfo=new info[in.length+count];//扩充数组
for(int j=0;j<in.length+count;j++)//初始化新数组
{
newinfo[j]=new info();
}
System.arraycopy(in,0,newinfo,0,in.length);//复制数组
for(int i=in.length;i<newinfo.length;i++)
{
newinfo[i].num=i;
System.out.println("请输入姓名:");
newinfo[i].name=sc.next();
System.out.println("请输入性别:");
newinfo[i].sex=sc.next();
System.out.println("请输入电话:");
newinfo[i].pnum=sc.nextInt();
System.out.println("请输入地址:");
newinfo[i].address=sc.next();
}
in=newinfo;//赋值覆盖
size=in.length;//更改数组长度
System.out.println("新增成功!!");
printall(in);
return in;
}
public void printall(info in[])
{
System.out.println("\t编号\t姓名\t性别\t电话\t通信地址 ");
for(int i=0;i<in.length;i++)
{
System.out.println("\t"+in[i].num+"\t"+in[i].name+"\t"+in[i].sex+"\t"+in[i].pnum+"\t"+in[i].address);
}
}
public static void main(String[] args)
{
Tongxun tx=new Tongxun();
System.out.println("请输入个数总数:");
int count=0;
Scanner sc=new Scanner(System.in);
count=sc.nextInt();
info in[]=new info[count];
for(int i=0;i<count;i++)
{
in[i]=new info();
}
tx.input(in);
tx.Addarr(in);
tx.printall(in);
/*while(1)
{
System.out.println("1.新增联系人");
System.out.println(".显示所有联系人");
System.out.println(".查询联系人");
System.out.println(".删除联系人");
}*/
}
}