java中实现向数组或list中添加不重复的值

来源:互联网 发布:坏男人网络征婚记 编辑:程序博客网 时间:2024/05/17 23:00

需求:向数组或list中添加一个数值,判断如果该数值已经存在,那么+1,继续想数组或list中添加,如果还存在则继续+1,直至该数值原数组或list中不存在位置

解决:List<String> acIdList = new ArrayList<String>();
                    for(int i=0;i<indexList.size();i++){
                        if(!indexList.get(i).getAcId().trim().isEmpty()){
                            acIdList.add(indexList.get(i).getAcId());
                        }
                    }
                    
                    while(acIdList.contains(acId)){
                        count++;
                        acId=flagZm+flagRq+df.format(count);
                    }

                   acId就是要产生的值

0 0
原创粉丝点击