Loading

将[4,3,2,5,4,3]分割成[4,3,2]、[5,4,3]两个List的算法

将[4,3,2,5,4,3]分割成[4,3,2]、[5,4,3]两个List的算法

package com.srie.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//将[4,3,2,5,4,3]分割成[4,3,2]、[5,4,3]两个List的算法;
public class TestSplitList {
    public static void main(String[] args) {
        // 原List中的对象元素的list
        List<Integer> list = Arrays.asList(new Integer[] { 4, 3, 2, 5, 4, 3 });
        System.out.println(list);
        T t = new T();
        t.setName("t");
        t.setList(list);
        // 原List
        List<T> tList = new ArrayList<T>();
        tList.add(t);
        // 新List
        List<T> newList = new ArrayList<T>();
        // 循环原List集合
        for (int i = 0, size = tList.size(); i < size; i++) {
            T t2 = tList.get(i);
            List<Integer> list2 = t2.getList();
            // 调用不断拆分的算法
            createNewList(newList, t2, list2, 0);
        }
        System.out.println(newList.size());
        System.out.println(newList.get(0).getList());
        System.out.println(newList.get(1).getList());
    }
    private static void createNewList(List<T> newList, T t2, List<Integer> list2, int i) {
        T t = new T();
        t.setName(t2.getName());
        List<Integer> list = new ArrayList<Integer>();
        t.setList(list);
        Integer shangyige = -1;
        for (int j = i, size = list2.size(); j < size; j++) {
            Integer dangqian = list2.get(j);
            if (j > i) {
                if (dangqian > shangyige) {
                    createNewList(newList, t, list2, j);
                    break;
                }
            }
            shangyige = dangqian;
            list.add(dangqian);
        }
        newList.add(t);
    }
}
class T {
    private String name;
    private List<Integer> list;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Integer> getList() {
        return list;
    }
    public void setList(List<Integer> list) {
        this.list = list;
    }
}

 

posted @ 2015-12-28 10:32  stono  阅读(226)  评论(0编辑  收藏  举报