请大大们先看代码:
    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;
}
    
现在我要写一个方法,删除指定的某条记录,要如何实现?import java.util.Scanner;import User.DbStore;public class CommunityManager 
{
public static void main(String args[])
{
delete();
}
public static void delete()
{
DbStore abcd = new DbStore();
Scanner scan = new Scanner(System.in);
System.out.println("请输入要删除的社区编号");
String deletingId = scan.next();
boolean judge = false;
for(int i=0;i<abcd.getCommArr().length;i++)
{
if(abcd.getCommArr()[i].getCommId().equals(deletingId))
{
System.out.println("您要删除的社区编号是:"+deletingId);
judge = true;
break;
}
}
if(!judge)
{
System.out.println("该社区不存在!");
}
}
}

解决方案 »

  1.   

    你可以不使用数组来保存JavaBean而是用List 这是JAVA推荐的解决方案,删除会很方便如果一定要用数组或者返回数组,那么也用List最后转换为数组就OK了你这里使用数组又回到C语言的时代了,删除一个数组元素 你是要后面元素全部前移么?还是只是做个标记删除?如果是前者那就费事了,后者你设置那个元素内容为null做个标记就OK
      

  2.   

    数组不可以删除。你可以用list或者用node实现链表。
      

  3.   


    import java.util.Arrays;public class Test {

    public int[] remove(int[] array,int index){
    int[] temp=new int[array.length-1];
    System.arraycopy(array, 0, temp, 0, index);
    System.arraycopy(array, index+1, temp, index, array.length-1-index);
    return temp;
    }

    public static void main(String[] args) {
    int[] array={2,5,3,7,4};
    int[] temp=new Test().remove(array, 3);
    System.out.println(Arrays.toString(temp));
    }
    }
    打印结果是:[2, 5, 3, 4]
      

  4.   


    package com.commd;public class CommunityManager {
    private CommunityEntity[] commArr;
    private int len=0;

     public CommunityManager(int max) //初始化用户实体数组
        {
      commArr=new CommunityEntity[max];
            CommunityEntity com1 = new CommunityEntity();
            com1.setCommId("001");
            com1.setCommName("新升新苑");
            com1.setCommManager("大漠");
            com1.setCommArea("高新区");
            com1.setCommStatue("可放装");
            com1.setPermeability(61);
            com1.setUserCount(221);
            insert(com1);
            
            CommunityEntity com2 = new CommunityEntity();
            com2.setCommId("002");
            com2.setCommName("新盛花园");
            com2.setCommManager("大漠");
            com2.setCommArea("吴中区");
            com2.setCommStatue("可放装");
            com2.setPermeability(62);
            com2.setUserCount(222);
            insert(com2);
            
            CommunityEntity com3 = new CommunityEntity();
            com3.setCommId("003");
            com3.setCommName("新唯家园");
            com3.setCommManager("大漠");
            com3.setCommArea("工业园区");
            com3.setCommStatue("可放装");
            com3.setPermeability(63);
            com3.setUserCount(223);
            insert(com3);
            
            CommunityEntity com4 = new CommunityEntity();
            com4.setCommId("004");
            com4.setCommName("蠡东花园");
            com4.setCommManager("大漠");
            com4.setCommArea("相城区");
            com4.setCommStatue("可放装");
            com4.setPermeability(64);
            com4.setUserCount(224);
            insert(com4);
            
            CommunityEntity com5 = new CommunityEntity();
            com5.setCommId("005");
            com5.setCommName("朱家庄新村");
            com5.setCommManager("大漠");
            com5.setCommArea("金阊区");
            com5.setCommStatue("可放装");
            com5.setPermeability(65);
            com5.setUserCount(225);
            insert(com5);
    //          setCommArr(new CommunityEntity[]{com1,com2,com3,com4,com5});
        }
        public void setCommArr(CommunityEntity[] commArr) 
        {
            this.commArr = commArr;
        }
        public CommunityEntity[] getCommArr()
        {
            return commArr;
        }
        
    public void insert(CommunityEntity value)
    {
    commArr[len++]=value;
    }
        
        public void deleteCommunityById(String commId)
        {
         for(int i=0;i<len;i++)
         {
         CommunityEntity ce=commArr[i];
         if(ce.getCommId().equals(commId))
         {
         for(int k=i;k<len;k++)
    {
         commArr[i]=commArr[i+1];
    }
    len--;
    return;
         }
        
        
         }
        
        }
        
        public void showCommunity()
        {
         for(int i=0;i<len;i++)
         {
         CommunityEntity ce=commArr[i];
         System.out.println(ce.getCommId()+" "+ce.getCommName());
         }
        }
        public static void main(String[] args) {
         CommunityManager cm=new CommunityManager(5);
         cm.deleteCommunityById("001");
         cm.showCommunity();
    }
    }
      

  5.   


    package com.commd;public class CommunityManager {
    private CommunityEntity[] commArr;
    private int len=0;

     public CommunityManager(int max) //初始化用户实体数组
        {
      commArr=new CommunityEntity[max];
            CommunityEntity com1 = new CommunityEntity();
            com1.setCommId("001");
            com1.setCommName("新升新苑");
            com1.setCommManager("大漠");
            com1.setCommArea("高新区");
            com1.setCommStatue("可放装");
            com1.setPermeability(61);
            com1.setUserCount(221);
            insert(com1);
            
            CommunityEntity com2 = new CommunityEntity();
            com2.setCommId("002");
            com2.setCommName("新盛花园");
            com2.setCommManager("大漠");
            com2.setCommArea("吴中区");
            com2.setCommStatue("可放装");
            com2.setPermeability(62);
            com2.setUserCount(222);
            insert(com2);
            
            CommunityEntity com3 = new CommunityEntity();
            com3.setCommId("003");
            com3.setCommName("新唯家园");
            com3.setCommManager("大漠");
            com3.setCommArea("工业园区");
            com3.setCommStatue("可放装");
            com3.setPermeability(63);
            com3.setUserCount(223);
            insert(com3);
            
            CommunityEntity com4 = new CommunityEntity();
            com4.setCommId("004");
            com4.setCommName("蠡东花园");
            com4.setCommManager("大漠");
            com4.setCommArea("相城区");
            com4.setCommStatue("可放装");
            com4.setPermeability(64);
            com4.setUserCount(224);
            insert(com4);
            
            CommunityEntity com5 = new CommunityEntity();
            com5.setCommId("005");
            com5.setCommName("朱家庄新村");
            com5.setCommManager("大漠");
            com5.setCommArea("金阊区");
            com5.setCommStatue("可放装");
            com5.setPermeability(65);
            com5.setUserCount(225);
            insert(com5);
    //          setCommArr(new CommunityEntity[]{com1,com2,com3,com4,com5});
        }
        public void setCommArr(CommunityEntity[] commArr) 
        {
            this.commArr = commArr;
        }
        public CommunityEntity[] getCommArr()
        {
            return commArr;
        }
        
    public void insert(CommunityEntity value)
    {
    commArr[len++]=value;
    }
        
        public void deleteCommunityById(String commId)
        {
         int i;
         for(i=0;i<len;i++)
         {
         CommunityEntity ce=commArr[i];
         if(ce.getCommId().equals(commId))
         {
         break;
         }
         }
        
         for(int k=i;k<len-1;k++)
    {
         commArr[k]=commArr[k+1];
    }
    len--;
    return;
        
        }
        
        public void showCommunity()
        {
         for(int i=0;i<len;i++)
         {
         CommunityEntity ce=commArr[i];
         System.out.println(ce.getCommId()+" "+ce.getCommName());
         }
        }
        public static void main(String[] args) {
         CommunityManager cm=new CommunityManager(5);
         cm.deleteCommunityById("002");
         cm.showCommunity();
    }
    }
    前面代码有点问题,重新调整过
      

  6.   

    list是一个集合,和数组相似,通过add和remove方法,可以很方便的增删元素。
      

  7.   

    把数组转换成LIST  然后重新创建一个List  进行删除操作 最后把List再转回数组仅供参考:
    String [] strs = new String[]{"1","2","3","4"};
    @SuppressWarnings("rawtypes")
    List list = Arrays.asList(strs);
    @SuppressWarnings("rawtypes")
    List list2 = new ArrayList<String>(list);
    list2.add("1");
    System.out.println(list2);
    @SuppressWarnings("unused")
    Object[] str2 = list2.toArray();
    String str3 = (String) str2[0];
    System.out.println(str3);
      

  8.   

    各位大大,你们的话我都研究了,最后我用的是标记删除,这个方法不是最好的但是对我这个阶段来说很合适,同时感谢zhucb2010,sasuke38,Rolos,代码我记下来了,以后再研究,真心感谢!
    if(abcd.getCommArr()[i].getCommId().equals(deletingId))
    {
    System.out.println("您要删除的社区编号是:"+deletingId);
    abcd.getCommArr()[i].setCommId(null);
    abcd.getCommArr()[i].setCommName(null);
    abcd.getCommArr()[i].setCommManager(null);
    abcd.getCommArr()[i].setCommArea(null);
    abcd.getCommArr()[i].setCommStatue(null);
    abcd.getCommArr()[i].setPermeability(0);
    abcd.getCommArr()[i].setUserCount(0);
    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()[i].getCommId()+"\t\t"+abcd.getCommArr()[i].getCommName()+"\t\t"+abcd.getCommArr()[i].getCommManager()+"\t\t"+abcd.getCommArr()[i].getCommArea()+"\t\t"+abcd.getCommArr()[i].getCommStatue()+"\t\t"+abcd.getCommArr()[i].getPermeability()+"%"+"\t\t"+abcd.getCommArr()[i].getUserCount());
    judge = true;
    break;
    }
      

  9.   

    http://blog.csdn.net/mengxiangyue/article/details/6876768看看这个 推荐使用集合类