posts - 55,comments - 89,trackbacks - 0

 

 1import java.util.ArrayList;
 2
 3public class Test01 {
 4    
 5    Test01() { }
 6
 7    /**
 8     * 除去字符串数组中重复次数超过整数n的数组元素.使用时请使用try{}catch(){}包含.或是使用throws声明抛出异常
 9     */

10    public String[] execute(String[] args, int n) throws Exception {
11        //Arrays.sort(args); // 此行代码可以不使用,使用得话会使数组工整,排序数据.
12        ArrayList<String> list = new ArrayList<String>(args.length);
13        //count变量,记录数组元素的重复次数
14        int count = 0;
15        for (int i = 0; i < args.length; i++{
16            count = 0;
17            for (int j = 0; j < args.length; j++{
18                if (args[i].equals(args[j])) {
19                    count++;
20                }

21            }

22            //把重复次数小于等于n的数组元素放进集合List中
23            if (count <= n) {
24                list.add(args[i]);
25            }

26
27        }

28        //新建一个数组,数组的长度为集合list的长度
29        String[] retStrs = new String[list.size()];
30        //然后把集合list的元素按照顺序赋给新建的数组
31        for (int i = 0; i < list.size(); i++{
32            retStrs[i] = (String) list.get(i);
33        }

34        //返回该数组
35        return retStrs;
36    }

37
38    public static void main(String[] arguments) throws Exception {
39
40        String[] t = new Test01().execute(new String[] "aaaaa""ccccc",
41                "aaaaa""aaaaa""ccccc""ddddd""ddddd""eeeee""aaaaa",
42                "fffff""ddddd""fffff""ddddd" }
3);
43        for (String x : t) {
44            System.out.println(x);
45        }

46    }

47}

 

posted on 2009-03-11 21:17 jiafang83 阅读(666) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: