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

Posted super超人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了'大''小'前后比较并归类相关的知识,希望对你有一定的参考价值。

\'大\'、\'小\'前后比较并归类,两种处理方式(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=小]]
           ]
         */
    }
}

 

以上是关于'大''小'前后比较并归类的主要内容,如果未能解决你的问题,请参考以下文章

php如何提取大括号中内容和参数 例如:name='abc' count='2'

279寻找比目标字母大的最小字母

279寻找比目标字母大的最小字母

djangoAPI返回json——前后端分离(查询图书)

PHP curl 上传大文件非常大慢,导致超时,小文件(10M以下)的还可以,请问如何解决啊!

微信小程序 uniapp wx.startLocationUpdateBackground前后台实时获取用户位置消息 获取用户使用设备的GPS开启状态