MyException - 我的异常网
当前位置:我的异常网» J2SE » 剔除数组中指定的某条记录

剔除数组中指定的某条记录(2)

www.MyException.Cn  网友分享于:2013-12-14  浏览:22次


如果一定要用数组或者返回数组,那么也用List最后转换为数组就OK了

你这里使用数组又回到C语言的时代了,删除一个数组元素 你是要后面元素全部前移么?还是只是做个标记删除?如果是前者那就费事了,后者你设置那个元素内容为null做个标记就OK
……

------解决方案--------------------
Java code

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]

------解决方案--------------------
Java code

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();
        }
}

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有