54
|
1 package jungle.test.functionalJava;
|
|
2
|
|
3 import java.util.Iterator;
|
|
4
|
|
5 import fj.P2;
|
|
6 import fj.data.List;
|
|
7
|
|
8 public class ListTest{
|
|
9
|
|
10 public static void main(String[] args) {
|
|
11 List<Integer> list = List.nil();
|
|
12 list = list.cons(1);
|
|
13 list = list.cons(2);
|
|
14 list = list.cons(4);
|
|
15 System.out.println("list :"+list);
|
|
16 System.out.println("list.index(0) :"+list.index(0));
|
|
17 System.out.println("list.head() :"+ list.head());
|
|
18 System.out.println("list.tail() :"+list.tail());
|
|
19 System.out.println("list.splitAt(2)");
|
|
20 P2<List<Integer>, List<Integer>> p2 = list.splitAt(2);
|
|
21 List<Integer> list1 = p2._1();
|
|
22 List<Integer> list2 = p2._2();
|
|
23 System.out.println(list1);
|
|
24 System.out.println(list2);
|
|
25
|
|
26 final int INSERT_NUM = 3;
|
|
27 list = insertInteger(list, INSERT_NUM);
|
|
28 System.out.println("newList :"+ list);
|
|
29 }
|
|
30
|
|
31 public static List<Integer> insertInteger(List<Integer> list, final int insertNum) {
|
|
32 int count = 0;
|
|
33 for(Iterator<Integer> iter = list.iterator(); iter.hasNext();){
|
|
34 int num = iter.next();
|
|
35 if(insertNum > num ) {
|
|
36 break;
|
|
37 }
|
|
38 count++;
|
|
39 }
|
|
40 P2<List<Integer>, List<Integer>> p2 = list.splitAt(count);
|
|
41 List<Integer> newList = List.nil();
|
|
42 newList = p2._2().cons(insertNum);
|
|
43 newList = p2._1().append(newList);
|
|
44 return newList;
|
|
45 }
|
|
46
|
|
47
|
|
48 }
|