java 笛卡尔集

今天测试过程中要造大量测试数据,就想着把几个集合里面的数据做个笛卡尔积,免得手动输入之苦。历时1小时,中间走了一些弯路。话不多说,上代码。
package demo.topTest.common;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * Created by liwenjing on 15/7/24.
 */
public class OriginData {

    public String[] address_option={"城市","机场"};
    public String[] cabin={"Y","X"};
    public String[] city={"北京","上海"};
    public String[] airport={"NAY","SHA"};
    public String[] arrcity={"纽约","东京"};

    private List<String>  mixData(List<String[]> oldlist){
        List<String> result=new LinkedList<String>();
        if(oldlist!=null) {
            copyArrayToList(result, oldlist.get(0));
            for(int i=1;i<oldlist.size();i++){
                ListIterator iterator= (ListIterator) result.iterator();
                while(iterator.hasNext()){
                    String oldString= (String) iterator.next();
                    iterator.remove();
                    for(int j=0;j<oldlist.get(i).length;j++){
                        iterator.add(oldString+oldlist.get(i)[j]);
                    }
                }
            }
        }
        return result;
    }

    private void copyArrayToList(List list,String[] array){
        for(int i=0;i<array.length;i++){
            list.add(array[i]);
        }
    }

    public void printList(List list){
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }

    public static  void main(String[] args){
        OriginData od=new OriginData();
        List<String[]> oldlist=new LinkedList<String[]>();
        oldlist.add(od.address_option);
        oldlist.add(od.cabin);
        oldlist.add(od.city);
        oldlist.add(od.airport);
        oldlist.add(od.arrcity);

        od.printList(od.mixData(oldlist));

    }
}



结果如图:
java 笛卡尔集
现在还是比较粗糙的。接下来要做的是从配置文件中读取数据,拼接好后放到csv或者xls里面,用作测试数据。

你可能感兴趣的:(java)