'大'、'小'前后比较并归类

'大'、'小'前后比较并归类,两种处理方式(1.前台;2.后台):

1.在前台处理:

<script type="text/javascript"> 
/*大、小前后比较并归类 start*/
var openNum1 = [
        {
            openNum1 : "小"
        },
        {
            openNum1 : "大"
        },
        {
            openNum1 : "小"
        },
        {
            openNum1 : "大"
        },
        {
            openNum1 : "小"
        },
        {
            openNum1 : "小"
        },
        {
            openNum1 : "小"
        }
]
var newArr = [];
var arr = [];
for(var i=0;i<openNum1.length;i++){
    debugger;
    if(i+1 == openNum1.length){
        arr.push(openNum1[i]);
        newArr.push(arr);
        break;
    }
    if(openNum1[i].openNum1 == openNum1[i+1].openNum1){
        arr.push(openNum1[i]);
    }else{
        arr.push(openNum1[i]);
        newArr.push(arr);
        //清空数组
        arr = new Array();
    }
    
}
/*大、小前后比较并归类 end*/
debugger;
</script>

结果:

2.在后台处理:

package test;

import java.util.ArrayList;
import java.util.List;

import com.test.www.entity.OpenNum1;
/**
 * 归类
 * @className GuiLei
 * @author liuyachao
 * @date 2018-1-12
 */
public class GuiLei {

    /**
     * @param args
     */
    public static void main(String[] args) {
        /**
         * 大、小前后比较并归类
         */
        guilei();
    }
    
    public static void guilei(){
        List<OpenNum1> openNum1 = new ArrayList<OpenNum1>();
        OpenNum1 p1 = new OpenNum1();
        p1.setOpenNum1("小");
        OpenNum1 p2 = new OpenNum1();
        p2.setOpenNum1("大");
        OpenNum1 p3 = new OpenNum1();
        p3.setOpenNum1("小");
        OpenNum1 p4 = new OpenNum1();
        p4.setOpenNum1("大");
        OpenNum1 p5 = new OpenNum1();
        p5.setOpenNum1("小");
        OpenNum1 p6 = new OpenNum1();
        p6.setOpenNum1("小");
        OpenNum1 p7 = new OpenNum1();
        p7.setOpenNum1("小");
        openNum1.add(p1);
        openNum1.add(p2);
        openNum1.add(p3);
        openNum1.add(p4);
        openNum1.add(p5);
        openNum1.add(p6);
        openNum1.add(p7);
        
        //新的list集合
        List newArr = new ArrayList(); 
        //临时的list集合
        List arr = new ArrayList(); 
        for(int i = 0;i<openNum1.size();i++){
            if(i+1 == openNum1.size()){
                arr.add(openNum1.get(i));
                newArr.add(arr);
                break;
            }
            if(openNum1.get(i).getOpenNum1() == openNum1.get(i+1).getOpenNum1()){
                arr.add(openNum1.get(i));
            }else{
                arr.add(openNum1.get(i));
                newArr.add(arr);
                //清空数组
                arr = new ArrayList();
            }
        }
        System.out.println(newArr);
        /* 结果:
         * [
             [OpenNum1 [openNum1=小]], 
             [OpenNum1 [openNum1=大]], 
             [OpenNum1 [openNum1=小]], 
             [OpenNum1 [openNum1=大]], 
             [OpenNum1 [openNum1=小], OpenNum1 [openNum1=小], OpenNum1 [openNum1=小]]
           ]
         */
    }
}

 

posted @ 2018-01-12 17:50  super超人  阅读(441)  评论(0编辑  收藏  举报