我写的代码。运行到所有元素输入完之后就没动静了,要么就抛异常,请大大们帮我看一下,谢谢package Community;import java.util.Scanner;import User.DbStore;public class temp 
{
public static void main(String args[])
{
DbStore abcd = new DbStore();

Scanner scan = new Scanner(System.in);

System.out.println("请输入小区编号:");
String id = scan.next();
System.out.println("请输入小区名称:");
String name = scan.next();
System.out.println("请输入小区经理:");
String manager = scan.next();
System.out.println("请输入所在区域:");
String area = scan.next();
System.out.println("请输入小区状态:");
String statue = scan.next();
System.out.println("请输入渗透率:");
double Permeability = scan.nextDouble();
System.out.println("请输入覆盖用户数:");
int userCount = scan.nextInt();

int last = abcd.getCommArr().length+1;

abcd.getCommArr()[last].setCommId(id);
abcd.getCommArr()[last].setCommName(name);
abcd.getCommArr()[last].setCommManager(manager);
abcd.getCommArr()[last].setCommArea(area);
abcd.getCommArr()[last].setCommStatue(statue);
abcd.getCommArr()[last].setPermeability(Permeability);
abcd.getCommArr()[last].setUserCount(userCount);

System.out.println("新增成功!");
System.out.println("该社区信息如下:"+"\n");
System.out.println("小区编号"+"\t\t"+"小区名称"+"\t\t"+"小区经理"+"\t\t"+"区域"+"\t\t"+"小区状态"+"\t\t"+"渗透率"+"\t\t"+"覆盖用户数");
System.out.println("----------------------------------------------------------------------------------------------------------");
System.out.println(abcd.getCommArr()[last].getCommId()+"\t\t"+abcd.getCommArr()[last].getCommName()+"\t\t"+abcd.getCommArr()[last].getCommManager()+"\t\t"+abcd.getCommArr()[last].getCommArea()+"\t\t"+abcd.getCommArr()[last].getCommStatue()+"\t\t"+abcd.getCommArr()[last].getPermeability()+"%"+"\t\t"+abcd.getCommArr()[last].getUserCount());
}
}

解决方案 »

  1.   

    想起来你另外一个帖子了,对于你用数组实现的话,是无法直接进行扩容的,你只能重新开辟一个更大的数组将原来的数据拷贝到更大的数组,然后将最新添加的数据放在更大数组尾部效率很低,还是用List合适
      

  2.   

    DbStore:
    package User;import Community.CommunityEntity;public class DbStore 
    {
       //声明一个数组变量
        private CommunityEntity[] commArr;    public DbStore() //初始化社区实体数组
        {
         CommunityEntity com1 = new CommunityEntity();
         com1.setCommId("001");
         com1.setCommName("新升新苑");
         com1.setCommManager("大漠");
         com1.setCommArea("高新区");
         com1.setCommStatue("可放装");
         com1.setPermeability(61);
         com1.setUserCount(221);
        
         CommunityEntity com2 = new CommunityEntity();
         com2.setCommId("002");
         com2.setCommName("新盛花园");
         com2.setCommManager("大漠");
         com2.setCommArea("吴中区");
         com2.setCommStatue("可放装");
         com2.setPermeability(62);
         com2.setUserCount(222);
        
         CommunityEntity com3 = new CommunityEntity();
         com3.setCommId("003");
         com3.setCommName("新唯家园");
         com3.setCommManager("大漠");
         com3.setCommArea("工业园区");
         com3.setCommStatue("可放装");
         com3.setPermeability(63);
         com3.setUserCount(223);
        
         CommunityEntity com4 = new CommunityEntity();
         com4.setCommId("004");
         com4.setCommName("蠡东花园");
         com4.setCommManager("大漠");
         com4.setCommArea("相城区");
         com4.setCommStatue("可放装");
         com4.setPermeability(64);
         com4.setUserCount(224);
        
         CommunityEntity com5 = new CommunityEntity();
         com5.setCommId("005");
         com5.setCommName("朱家庄新村");
         com5.setCommManager("大漠");
         com5.setCommArea("金阊区");
         com5.setCommStatue("可放装");
         com5.setPermeability(65);
         com5.setUserCount(225);
      
            setCommArr(new CommunityEntity[]{com1,com2,com3,com4,com5});
        }
    public void setCommArr(CommunityEntity[] commArr) 
    {
    this.commArr = commArr;
    }
    public CommunityEntity[] getCommArr()
    {
    return commArr;
    }
    }
    CommunityEntity:package Community;public class CommunityEntity 
    {
    private String commId;
    private String commName;
    private String commManager;
    private String commArea;
    private String commStatue;
    private double permeability;
    private int userCount;
    public String getCommId() 
    {
    return commId;
    }
    public void setCommId(String commId)
    {
    this.commId = commId;
    }
    public String getCommName() 
    {
    return commName;
    }
    public void setCommName(String commName) 
    {
    this.commName = commName;
    }
    public String getCommManager() 
    {
    return commManager;
    }
    public void setCommManager(String commManager)
    {
    this.commManager = commManager;
    }
    public String getCommArea()
    {
    return commArea;
    }
    public void setCommArea(String commArea)
    {
    this.commArea = commArea;
    }
    public String getCommStatue() 
    {
    return commStatue;
    }
    public void setCommStatue(String commStatue) 
    {
    this.commStatue = commStatue;
    }
    public double getPermeability() 
    {
    return permeability;
    }
    public void setPermeability(double permeability)
    {
    this.permeability = permeability;
    }
    public int getUserCount() 
    {
    return userCount;
    }
    public void setUserCount(int userCount) 
    {
    this.userCount = userCount;
    }
    }
    类们都在这了。求帮助
      

  3.   

    我只是学习一下。list什么的,没资料学。。
      

  4.   

    新增元素不能这么写的...
    public  static int[] add(int[] arr,int b)
    {
    int[] c = new int[arr.length+1];
    for(int i=0;i<arr.length;i++)
    {
    c[i]=arr[i];
    }
    c[arr.length] = b;
    return c;
    }
    每次增加一个元素,新建一个数组赋值,返回值就是你要的
    或者你一开始就新建一个足够大的数组,然后设置一个size标量表示有多少个元素,添加的时候判断size++比数组length小就放入那样的
      

  5.   

    我用java里面的List重新给你写了一个实现,还有提醒你看看Java编码规范,类名首字母一定大写,包的首字母不大写,还有局部变量的名称首字母也不大写,三个类放在了一个包下面package csdn.p31;public class CommunityEntity
    {
        private String commId;
        private String commName;
        private String commManager;
        private String commArea;
        private String commStatue;
        private double permeability;
        private int userCount;    public CommunityEntity(){}    @Override
        public String toString() {
            return "CommunityEntity{" +
                    "commId='" + commId + '\'' +
                    ", commName='" + commName + '\'' +
                    ", commManager='" + commManager + '\'' +
                    ", commArea='" + commArea + '\'' +
                    ", commStatue='" + commStatue + '\'' +
                    ", permeability=" + permeability +
                    ", userCount=" + userCount +
                    '}';
        }    public String getCommId()
        {
            return commId;
        }
        public void setCommId(String commId)
        {
            this.commId = commId;
        }
        public String getCommName()
        {
            return commName;
        }
        public void setCommName(String commName)
        {
            this.commName = commName;
        }
        public String getCommManager()
        {
            return commManager;
        }
        public void setCommManager(String commManager)
        {
            this.commManager = commManager;
        }
        public String getCommArea()
        {
            return commArea;
        }
        public void setCommArea(String commArea)
        {
            this.commArea = commArea;
        }
        public String getCommStatue()
        {
            return commStatue;
        }
        public void setCommStatue(String commStatue)
        {
            this.commStatue = commStatue;
        }
        public double getPermeability()
        {
            return permeability;
        }
        public void setPermeability(double permeability)
        {
            this.permeability = permeability;
        }
        public int getUserCount()
        {
            return userCount;
        }
        public void setUserCount(int userCount)
        {
            this.userCount = userCount;
        }
    }package csdn.p31;import java.util.List;
    import java.util.ArrayList;public class DbStore
    {
        public List<CommunityEntity> getCommList() {
            return commList;
        }//声明一个数组变量    public void setCommList(List<CommunityEntity> commList) {
            this.commList = commList;
        }    private List<CommunityEntity> commList=new ArrayList<CommunityEntity>();    public DbStore() //初始化社区实体数组
        {
           
            CommunityEntity com1 = new CommunityEntity();
            com1.setCommId("001");
            com1.setCommName("新升新苑");
            com1.setCommManager("大漠");
            com1.setCommArea("高新区");
            com1.setCommStatue("可放装");
            com1.setPermeability(61);
            com1.setUserCount(221);        CommunityEntity com2 = new CommunityEntity();
            com2.setCommId("002");
            com2.setCommName("新盛花园");
            com2.setCommManager("大漠");
            com2.setCommArea("吴中区");
            com2.setCommStatue("可放装");
            com2.setPermeability(62);
            com2.setUserCount(222);        CommunityEntity com3 = new CommunityEntity();
            com3.setCommId("003");
            com3.setCommName("新唯家园");
            com3.setCommManager("大漠");
            com3.setCommArea("工业园区");
            com3.setCommStatue("可放装");
            com3.setPermeability(63);
            com3.setUserCount(223);        CommunityEntity com4 = new CommunityEntity();
            com4.setCommId("004");
            com4.setCommName("蠡东花园");
            com4.setCommManager("大漠");
            com4.setCommArea("相城区");
            com4.setCommStatue("可放装");
            com4.setPermeability(64);
            com4.setUserCount(224);        CommunityEntity com5 = new CommunityEntity();
            com5.setCommId("005");
            com5.setCommName("朱家庄新村");
            com5.setCommManager("大漠");
            com5.setCommArea("金阊区");
            com5.setCommStatue("可放装");
            com5.setPermeability(65);
            com5.setUserCount(225);//        setCommArr(new CommunityEntity[]{com1,com2,com3,com4,com5});
            commList.add(com1);
            commList.add(com2);
            commList.add(com3);
            commList.add(com4);
            commList.add(com5);    }}package csdn.p31;import java.util.Scanner;
    import java.util.List;public class Temp 
    {
        public static void main(String args[])
        {
            DbStore store= new DbStore();        Scanner scan = new Scanner(System.in);        System.out.println("请输入小区编号:");
            String id = scan.next();
            System.out.println("请输入小区名称:");
            String name = scan.next();
            System.out.println("请输入小区经理:");
            String manager = scan.next();
            System.out.println("请输入所在区域:");
            String area = scan.next();
            System.out.println("请输入小区状态:");
            String statue = scan.next();
            System.out.println("请输入渗透率:");
            double permeability = scan.nextDouble();
            System.out.println("请输入覆盖用户数:");
            int userCount = scan.nextInt();        List<CommunityEntity> list=store.getCommList();
            CommunityEntity ce=new CommunityEntity();
            ce.setCommId(id);
            ce.setCommName(name);
            ce.setCommManager(manager);
            ce.setCommArea(area);
            ce.setCommStatue(statue);
            ce.setPermeability(permeability);
            ce.setUserCount(userCount);
            list.add(ce);        System.out.println("新增成功!");
            System.out.println("该社区信息如下:"+"\n");
            System.out.println(ce);
    //        System.out.println("小区编号"+"\t\t"+"小区名称"+"\t\t"+"小区经理"+"\t\t"+"区域"+"\t\t"+"小区状态"+"\t\t"+"渗透率"+"\t\t"+"覆盖用户数");
    //        System.out.println("----------------------------------------------------------------------------------------------------------");
            for(CommunityEntity c:list){
                System.out.println("info:"+c);
            }
            
        }
    }
      

  6.   

    新增部分已经做完了,改了一下,很好用。我再试试删除部分也用list,感谢你,熊猫哥