随笔 - 55  文章 - 187  trackbacks - 0
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(12)

随笔分类

随笔档案

groovy

搜索

  •  

最新评论

阅读排行榜

评论排行榜

     摘要: 页面代码:  1<html>  2    <head>  3        <meta http-equiv="Content-Type" content="text/html; ch...  阅读全文
posted @ 2008-03-07 16:11 大卫 阅读(5061) | 评论 (1)编辑 收藏
 1package test;
 2
 3import java.lang.reflect.Method;
 4import java.lang.reflect.ParameterizedType;
 5import java.lang.reflect.Type;
 6import java.util.List;
 7import java.util.Map;
 8
 9public class TempTest {
10
11    public static void main(String[] args) throws Exception {
12        Method[] methods = TempTest.class.getDeclaredMethods();
13        for (Method method : methods) {
14            System.out.println("method:" + method.getName());// 方法名
15
16            // //////////////方法的参数
17            System.out.println(" paramTypeType: ");
18            Type[] paramTypeList = method.getGenericParameterTypes();// 方法的参数列表
19            for (Type paramType : paramTypeList) {
20                System.out.println("  " + paramType);// 参数类型
21                if (paramType instanceof ParameterizedType)/* 如果是泛型类型 */{
22                    Type[] types = ((ParameterizedType) paramType)
23                            .getActualTypeArguments();// 泛型类型列表
24                    System.out.println("  TypeArgument: ");
25                    for (Type type : types) {
26                        System.out.println("   " + type);
27                    }

28                }

29            }

30
31            // //////////////方法的返回值
32            System.out.println(" returnType: ");
33            Type returnType = method.getGenericReturnType();// 返回类型
34            System.out.println("  " + returnType);
35            if (returnType instanceof ParameterizedType)/* 如果是泛型类型 */{
36                Type[] types = ((ParameterizedType) returnType)
37                        .getActualTypeArguments();// 泛型类型列表
38                System.out.println("  TypeArgument: ");
39                for (Type type : types) {
40                    System.out.println("   " + type);
41                }

42            }

43
44        }

45
46    }

47
48    public static String method1(List list) {
49        return null;
50    }

51
52    private static Map<String, Double> method2(Map<String, Object> map) {
53        return null;
54    }

55
56}
posted @ 2008-02-28 10:29 大卫 阅读(5790) | 评论 (3)编辑 收藏

算法程序题:

    该公司笔试题就1个,要求在10分钟内作完。

    题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。

  基本思路:
1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。
2 显然这个结果集还未达到题目的要求。从以下几个方面考虑:
  1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
  2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果。//TreeSet用于过滤一个集合中相同的东西还真是个挺不错的方法
  3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。

采用二维数组定义图结构,最后的代码是:

 1package test;
 2
 3import java.util.Iterator;
 4import java.util.TreeSet;
 5
 6public class TestQuestion {
 7
 8    private String[] b = new String[] "1""2""2""3""4""5" };
 9    private int n = b.length;
10    private boolean[] visited = new boolean[n];
11    private int[][] a = new int[n][n];
12    private String result = "";
13    private TreeSet treeSet = new TreeSet();// 用于保存结果,具有过滤相同结果的作用。
14
15    public static void main(String[] args) {
16        new TestQuestion().start();
17    }

18
19    private void start() {
20        // 创建合法路径标识集合
21        for (int i = 0; i < n; i++{
22            for (int j = 0; j < n; j++{
23                if (i == j) {
24                    a[i][j] = 0;
25                }
 else {
26                    a[i][j] = 1;
27                }

28            }

29        }

30        a[3][5= 0;
31        a[5][3= 0;
32        for (int i = 0; i < n; i++{
33            this.depthFirstSearch(i);// 深度递归遍历
34        }

35        Iterator it = treeSet.iterator();
36        while (it.hasNext()) {
37            String string = (String) it.next();
38
39            if (string.indexOf("4"!= 2{
40                System.out.println(string);
41            }

42        }

43    }

44
45    /**
46     * 深度优先遍历
47     * 
48     * @param startIndex
49     */

50    private void depthFirstSearch(int startIndex) {
51        // 递归的工作
52        visited[startIndex] = true;// 用于标识已经走过的节点
53        result = result + b[startIndex];// 构造结果
54        if (result.length() == n) {
55            treeSet.add(result);// 添加到TreeSet类型中,具有过滤相同结果的作用
56        }

57        // 每走到一个节点,挨个遍历下一个节点
58        for (int j = 0; j < n; j++{
59            if (a[startIndex][j] == 1 && visited[j] == false{
60                depthFirstSearch(j);// 深度递归遍历
61            }
 else {
62                continue;
63            }

64        }

65        // 递归的收尾工作
66        result = result.substring(0, result.length() - 1);
67        visited[startIndex] = false;// 取消访问标识
68    }

69}

70

--------------------

    WE准高手
posted @ 2008-02-27 14:30 大卫 阅读(2982) | 评论 (12)编辑 收藏
TreeSet类型是J2SE中唯一可实现自动排序的类型,用法如下:

MyComparator.java
 1package test;
 2
 3import java.util.Comparator;
 4
 5public class MyComparator<T> implements Comparator<T> {
 6
 7    public int compare(T arg0, T arg1) {
 8        if (arg0.equals(arg1)) {
 9            return 0;
10        }

11        return ((Comparable<T>) arg0).compareTo(arg1) * -1;
12    }

13
14}

TreeSetTest.java
 1package test;
 2
 3import java.util.Iterator;
 4import java.util.TreeSet;
 5
 6public class TreeSetTest {
 7
 8    /**
 9     * @param args
10     */

11    public static void main(String[] args) {
12
13        MyComparator<String> myComparator = new MyComparator<String>();
14
15        // /////////////////////不添加自定义排序
16        TreeSet<String> treeSet1 = new TreeSet<String>();
17        treeSet1.add("c");
18        treeSet1.add("a");
19        treeSet1.add("b");
20
21        Iterator<String> iterator1 = treeSet1.iterator();
22        while (iterator1.hasNext()) {
23            System.out.println(iterator1.next());
24        }

25
26        // /////////////////////添加自定义排序
27        TreeSet<String> treeSet2 = new TreeSet<String>(myComparator);
28        treeSet2.add("c");
29        treeSet2.add("a");
30        treeSet2.add("b");
31
32        Iterator<String> iterator2 = treeSet2.iterator();
33        while (iterator2.hasNext()) {
34            System.out.println(iterator2.next());
35        }

36    }

37
38}

39

运行结果:
a
b
c
c
b
a

--------------------

    WE准高手
posted @ 2008-02-27 13:34 大卫 阅读(8415) | 评论 (3)编辑 收藏
假设要添加库文件richfaces-ui-3.1.3.GA.jar
1、为库richfaces-ui-3.1.3.GA.jar文件建立pom文件richfaces-ui-3.1.3.GA.pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.richfaces</groupId>
 <artifactId>richfaces-ui</artifactId>
 <version>3.1.3.GA</version>
 <name>RichFaces JSF components library</name>
 <packaging>jar</packaging>
</project>
2、用ant为jar和pom文件分别生成校验文件.sha1
<project default="main">
 <target name="main" description="Generate checksum file for jar and pom">  
       <checksum algorithm="SHA" fileext=".sha1">  
           <fileset dir="F:/software/java/richfaces-ui-3.1.3.GA/lib" id="id">   
              <include name="**/*.pom" />  
              <include name="**/*.jar" />  
              <include name="**/*.xml" />  
              <exclude name="**/*.sh1" />  
           </fileset>  
       </checksum>  
 </target>
</project>
3、在.m2目录中创建该库文件的代码库目录.m2\repository\org\richfaces\richfaces-ui\3.1.3.GA
其中,org\richfaces为包路径,richfaces-ui为包名,3.1.3.GA为版本,这三项是与pom文件中的groupId,artifactId,version分别对应的。
richfaces-ui-3.1.3.GA.jar,richfaces-ui-3.1.3.GA.jar.sha1,richfaces-ui-3.1.3.GA.pom,richfaces-ui-3.1.3.GA.pom.sha1拷贝到该目录下。
4、在工程的pom文件中添加依赖
<dependency>
 <groupId>org.richfaces</groupId>
 <artifactId>richfaces-ui</artifactId>
 <version>3.1.3.GA</version>
</dependency>

--------------------

    WE准高手
posted @ 2008-02-12 15:07 大卫 阅读(1439) | 评论 (0)编辑 收藏
仅列出标题
共10页: First 上一页 2 3 4 5 6 7 8 9 10 下一页