Java到Groovy初瞰_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java到Groovy初瞰

Java到Groovy初瞰

 2011/11/18 7:58:58  greatwqs  http://greatwqs.iteye.com  我要评论(0)
  • 摘要:1.用java初始化一个ArrayList,中间处理最后输出:importjava.util.List;importjava.util.ArrayList;classErase{privateListfilterLongerThan(Liststrings,intlength){Listresult=newArrayList();for(inti=0;i<strings.size();i++){Strings=(String)strings.get(i);if(s.length()<
  • 标签:Java

1. 用java初始化一个ArrayList,中间处理最后输出:

import java.util.List;
import java.util.ArrayList;

class Erase {
	private List filterLongerThan(List strings, int length) {
		List result = new ArrayList();
		for (int i = 0; i < strings.size(); i++) {
			String s = (String) strings.get(i);
			if (s.length() <= length) {
				result.add(s);
			}
		}
		return result;
	}

	public static void main(String[] args) {
		List names = new ArrayList();
		names.add("Ted");
		names.add("Fred");
		names.add("Jed");
		names.add("Ned");
		System.out.println(names);
		Erase e = new Erase();
		List shortNames = e.filterLongerThan(names, 3);
		System.out.println(shortNames.size());
		for (int i = 0; i < shortNames.size(); i++) {
			String s = (String) shortNames.get(i);
			System.out.println(s);
		}
	}
}

?

?

执行结果:

[Ted, Fred, Jed, Ned]
3
Ted
Jed
Ned

?

?

2. 经过Groovy语法,处理分号,处理import包.

代码如下:

class Erase {
	private List filterLongerThan(List strings, intlength) {
	    List result = new ArrayList()
	    for (String s in strings) {
	        if (s.length() <= length) {
	            result.add(s)
	        }
	    }
	    return result
	}
	public static void main(String[] args) {
	    List names = new ArrayList()
	    names.add("Ted"); names.add("Fred")
	    names.add("Jed"); names.add("Ned")
	    System.out.println(names)
	    Erase e = new Erase()
	    List shortNames= e.filterLongerThan(names, 3)
	    System.out.println(shortNames.size())
	    for (String s in shortNames) {
	        System.out.println(s)
	    }
	}
}

?

?

3.? 处理类的声明, 处理变量的类型,和函数的用法;

代码如下:

filterLongerThan(strings, length) {
	def result = new ArrayList()
	for (s in strings) {
		if (s.size() <= length) {
			result.add(s)
			}
		}
	return result
}
    names = new ArrayList()
    names.add("Ted")
    names.add("Fred")
    names.add("Jed")
    names.add("Ned")
    System.out.println(names)
    shortNames= filterLongerThan(names, 3)
    System.out.println(shortNames.size())
    for (s in shortNames) {
    	System.out.println(s)
    }

?

?

4. 处理List的初始化,添加,遍历操作.

代码如下:

def filterLongerThan(strings, length) {
	return strings.findAll{ it.size() <= length }
}
	names = ["Ted", "Fred", "Jed", "Ned"]
	System.out.println(names)
	shortNames= filterLongerThan(names, 3)
	System.out.println(shortNames.size())
	shortNames.each{ System.out.println(s)}

?

5. 简化函数filterLongerThan, 最简单的groovy代码如下:

names =	["Ted", "Fred", "Jed", "Ned"]
println names
shortNames= names.findAll{ it.size() <= 3 }
println shortNames.size()
shortNames.each{ println it }

?

运行groovy结果:

[Ted, Fred, Jed, Ned]
3
Ted
Jed
Ned

?

遵循groovy简洁语法! 简单开发java.

发表评论
用户名: 匿名