首页 > 技术文章 > 把list(对象)集合中的(某个属性),放到数组中。

yunqing 2017-07-20 09:44 原文

List<SpecialguardInfo> list=specialguardOrderService.findfreeSg(date1,date2);//得到list对象集合
        String[] arr=new String[list.size()];
        //把list对象中的id属性装进arr数组
        int count=0;
        for (SpecialguardInfo sp : list) {
            arr[count]=sp.getId();
            count++;
        }
        String[] array=specialguardId.split(",");//分割传入id
        boolean boo=false;
        String strName="";//姓名
        
        //判断选择是否有不在arr数组中的id号
        for (String string : array) {
//这句代码就是判断array数组中的每个元素是否在一个list集合中,因为要得到list集合中的id属性才装进一个arr数组中,
//使用Arrays.asList(arr)转list格式进行判断。 boo
=Arrays.asList(arr).contains(string);//判断array中的元素是否在arr数组中 if(boo==false){//如果不在 strName=specialguardOrderService.findSgNameById(string);//获取string号人的name break;//直接跳出循环 } } if(boo==false){ map.put("msg", strName+"不在空闲时间内,请重新选择"); map.put("code",208);//获取成功 }

 删除多个特卫人员的时候,要逐个进行判断此人是否还有未完成的预约订单,如果有,要警告再次确认才能删除,这其中也用到了这个判断。

@RequestMapping(value="delSpecialguardInfo",method=RequestMethod.POST)
    public void delSpecialguardInfo(HttpServletRequest request,HttpServletResponse response,@RequestParam("ids")String ids) throws IOException{
        
        Map<String,Object> status=new HashMap<String,Object>();
        if(ids!=null&&!"".equals(ids)){
            String arr[]=ids.split(":");//分割多个人员id,debug看到id之间用的:隔开
            boolean boo=false;
            String strName="";
            List<String> list=specialguardService.findSgIdFormOrder();//查询所有还有预约的特位 id
            for (String string : arr) {
                //一个个判断保存
                SpecialguardInfo order=specialguardService.findSpecialguardInfoById(string);//用来获取此特位信息,用于保存。
                boo=list.contains(string);//判断string变量代表的id号是否在list中,list代表有预约的特位
                //查出这个特卫的姓名
                SpecialguardInfo sg=specialguardService.findSpecialguardInfoById(string);
                strName=sg.getName();
                if(boo){
            //这是一个后台强行在前端页面弹出警告框的操作,慎用。。
int res = JOptionPane.showConfirmDialog(null, strName+"这个特位还有预约尚未完成,确定删除?", "警告", JOptionPane.YES_NO_OPTION); if (res == JOptionPane.YES_OPTION) {// 点击“是”后执行这个代码块
               //执行删除才做后保存。此处删除是假删除,改编状态而已
order.set_state(0); specialguardService.save(order); status.put("status", 200); } else {
              //不删除,状态保持不变,1代表未删除0代表已删除 order.set_state(
1); specialguardService.save(order); status.put("status", 200); } }else{
            //boo==false代表没有订单的特卫,可以不用提示直接删除。 order.set_state(
0); specialguardService.save(order); status.put("status", 200); } } }else{ status.put("status", 201); } response.setHeader("Access-Control-Allow-Origin","*"); response.setHeader("Access-Control-Allow-Methods","GET,POST"); }

 

推荐阅读