Mercurial > hg > Members > nobuyasu > jungle-network
view src/jungle/test/functionalJava/ListTest.java @ 54:3d78d438f97b
add ListTest
author | one |
---|---|
date | Sun, 14 Jul 2013 17:21:37 +0900 |
parents | |
children | ccfe9b5e8f11 |
line wrap: on
line source
package jungle.test.functionalJava; import java.util.Iterator; import fj.P2; import fj.data.List; public class ListTest{ public static void main(String[] args) { List<Integer> list = List.nil(); list = list.cons(1); list = list.cons(2); list = list.cons(4); System.out.println("list :"+list); System.out.println("list.index(0) :"+list.index(0)); System.out.println("list.head() :"+ list.head()); System.out.println("list.tail() :"+list.tail()); System.out.println("list.splitAt(2)"); P2<List<Integer>, List<Integer>> p2 = list.splitAt(2); List<Integer> list1 = p2._1(); List<Integer> list2 = p2._2(); System.out.println(list1); System.out.println(list2); final int INSERT_NUM = 3; list = insertInteger(list, INSERT_NUM); System.out.println("newList :"+ list); } public static List<Integer> insertInteger(List<Integer> list, final int insertNum) { int count = 0; for(Iterator<Integer> iter = list.iterator(); iter.hasNext();){ int num = iter.next(); if(insertNum > num ) { break; } count++; } P2<List<Integer>, List<Integer>> p2 = list.splitAt(count); List<Integer> newList = List.nil(); newList = p2._2().cons(insertNum); newList = p2._1().append(newList); return newList; } }