java 的一个关于数组的问题
package shiyan2_2;
import java.util.*;
//import java.io.*;
class info
{
int num;
String name;
String sex;
int pnum;
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 void 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];//扩充数组
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("新增成功!!");
}
public void printall(info in[])
{
System.out.println("编号 姓名 性别 电话通信地址 ");
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];
tx.input(in);
tx.Addarr(in);
tx.printall(in);
/*while(1)
{
System.out.println("1.新增联系人");
System.out.println(".显示所有联系人");
System.out.println(".查询联系人");
System.out.println(".删除联系人");
}*/
}
}