2004年7月23日

在Java、C#和C++中遍历集合

在Java中,常见的遍历集合方式如下:

Iterator iter = list.iterator();
while (iter.hasNext()) {
 Object item 
= iter.next();
}

也可以使用for

for (Iterator iter = list.iterator(); iter.hasNext()) {
   Object item 
= iter.next();
}


JDK 1.5引入的增强的for语法

List list = 
for (Integer item : list) {
}

在C#中,遍历集合的方式如下:

foreach (Object item in list) 
{
}

其实你还可以这样写,不过这样写的人很少而已

IEnumerator e = list.GetEnumerator();
while (e.MoveNext()) 
{
 Object item 
= e.Current;
}

在C# 2.0中,foreach能够作一定程度的编译期类型检查。例如:

IList<int> intList = 
foreach(String item in intList) { } //编译出错


在C++标准库中。for_each是一种算法。定义如下:
for_each(InputIterator beg, InputIterator end, UnaryProc op)
C++中,由于能够重载运算符(),所以有一种特殊的对象,仿函数。

template<class T>
class AddValue {
    
private:
        T theValue;
    
public:
        AddValue(
const T& v) : theValue(v) {
        }

        
        
void operator() (T& elem) const {
            elem 
+= theValue;
        }

}
;

vector
<int> v;
INSERT_ELEMENTS(v, 
19);

for_each (v.begin(), v.end(), AddValue
<int>(10));

posted @ 2004-07-23 23:53 温少 阅读(2770) 评论(3) 编辑

关于Update语句在不同数据库中的差别

Oralce和DB2都支持的语法:
UPDATE A 
 
SET (A1, A2, A3) = (SELECT B1, B2, B3 FROM B WHERE A.ID = B.ID)

MS SQL Server不支持这样的语法,相对应的写法为:
UPDATE A 
    
SET A1 = B1, A2 = B2, A3 = B3 
    
FROM A LEFT JOIN B ON A.ID = B.ID

个人感觉MS SQL Server的Update语法功能更为强大。MS SQL SERVER的写法:
UPDATE A 
    
SET A1 = B1, A2 = B2, A3 = B3
    
FROM A, B WHERE A.ID = B.ID

在Oracle和DB2中的写法就比较麻烦了,如下:
UPDATE A 
    
SET (A1, A2, A3) = (SELECT B1, B2, B3 FROM B WHERE A.ID = B.ID)
    
WHERE ID IN (SELECT B.ID FROM B WHERE A.ID = B.ID)

posted @ 2004-07-23 20:06 温少 阅读(15659) 评论(30) 编辑

BPEL4WS的开源Java实现

ActiveBPEL
http://www.activebpel.org/info/intro.html


BPEL4WS的开源实现,协议是GPL。关注external workflow的朋友可以留意一下。

Java的开源项目真多,还是那样的感觉,拥抱Java,拥抱开放!

posted @ 2004-07-23 19:37 温少 阅读(1485) 评论(1) 编辑

一个对于博客园的建议

博客园办得很不错。

我一些朋友,他们熟悉J2EE,觉得博客园的技术氛围好,也想来申请,但觉得博客园标榜专注于.NET技术,所以不便申请。

大家是否也觉得“专注于.NET技术”说法,有拒人与门外之意呢?

为引起大家的注意,把此文章发到首页技术区,大家请见谅!

posted @ 2004-07-23 01:55 温少 阅读(1362) 评论(21) 编辑

关于Page.cs文件中注释的一点补充

突然想起,所以写出来,以方便大家阅读我提供的ASP .NET资料。
Page.cs文件中,我写了一些注释,但有一个很重要的地方有遗漏。

大家分析Page.cs文件时,可能会有一个疑问,aspx中的那些Controls怎么装载的呢?

答案在:
private void ProcessRequest()方法中的

this.FrameworkInitialize();

你在
%System%\Microsoft.NET\Framework\v1.1.4322\下的ASP .NET临时目录下,会看到一些ASPX动态生成的.cs文件,通过观看这些cs文件中的FrameworkInitialize()方法的实现,就会知道这个方法是干什么的。而且你通过这个方法的一些具体实现,会学习到一些特别的WebControl的用法。
你构建一个使用DataGrid的TemplateColumn的ASPX文件,然后查看动态生成的.cs文件,你会学习到TemplateColumn的一些特别用法,这是MSDN文档中没有介绍的。通过这些技巧,你就可能把WebControl使用得出神入化!

我相信我提供的ASP .NET资料中,会对大家学习编写WebControl和深入理解ASP .NET有帮助,希望有人能够写一些分析文章出来,期待中……

posted @ 2004-07-23 01:43 温少 阅读(732) 评论(2) 编辑

关于使用for循环

有人喜欢使用for作类似while的循环:

for(;;) { }

评论:这是一种风格,有一部分人使用,我个人觉得不如while来得直观。

究竟是++i,还是i++

for (int i = 0; i < arrayA.Length; ++i) { }

for (int i = 0; i < arrayA.Length; i++{ }

以前我使用i++的写法,后来,看到一些优秀的代码中,大多使用++i,我就改用++i了。为什么使用++i,看到过一些理由,但觉得那些理由,不值一提。

使用i, j, k作多层循环

for (int i = 0; i < arrayA.Length; ++i) 
{
  Object[] arrayB 
= arrayA[i];
    
for (int j = 0; j < arrayB.Length; ++j)
    
{
        Object b 
= arrayB[j]; //此处需要多加注意,一不留神就会写成Object b = arrayB[i];
    }
 
}

使用i, j, k做多层循环时,很容易犯错误,如上述代码中的注释缩写的那样。我参加的代码评审(Code Review)中,或者开发过程中,经常有同事遇到这种BUG。我也曾经吃过亏,每次编写i, j, k多层for循环时,就会提醒自己,小心,别犯错误!

使用for的习惯,除了++i和i++的写法不同外,还有和if、while等语句一样的不同写法,如下:

第一种写法
a、关键字for后面有空格
b、二元表达式左右有空格
c、每个大括号,单独占一行
d、else关键字单独占一行

for (int i = 0; i < list.Count; ++i)
{
}


第二种写法:
a、关键字if后面有空格
b、二元表达式左右有空格
c、大括号放在上一句的右边

for (int i = 0; i < list.Count; ++i) {
}


需要优化是,可能你也会采用这种写法:

int listCount = list.Count;
for (int i = 0; i < listCount; ++i) { }

如果for循环中,不会添加或者删除list的元素,使用这种方式优化,可能得到一丁点(可能不值一提)的性能提升。

需要从list中删除元素时,可能你会使用反序的遍历。如下:

int listCount = list.Count;
for (int i = listCount - 1; i >= 0--i) 
{
    
if (condition) 
    
{
        list.RemoveAt(i);
    }

}

如果list的实现是ArrayList,反序遍历可能比正序删除的性能更高一些。

此外,for还有一些其他的使用方式,如:

for (init(); condition(); incement()) { }
for (; i < j; ++i, --j) { };

posted @ 2004-07-23 01:27 温少 阅读(1996) 评论(10) 编辑

SAP工作流中DialogWorkItem的状态

SAP毕竟是ERP软件的龙头,其工作流作得相当不错,和其业务系统集成得相当好。

SAP的工作流中,WorkItem的类型有很多,其中一种是Dialog WorkItem,用于人工参与的任务。其任务的状态分类,也许值得我们设计工作流引擎参考。如下: 



waiting
The work item has been scheduled for its requested start.
A work item has this status
 if it already exists but the requested start specified in the workflow definition has not been reached yet.
 if it has been set to resubmission Work items in the waiting status are not displayed in the workflow inbox.

ready
The work item has been released for execution and appears in the workflow inbox of all recipients.

reserved
The work item has been received by one of its recipients with the result that its status has changed from ready to reserved.
A work item in the reserved status is then displayed to this recipient only. It is no longer displayed in the workflow inboxes of the other recipients.

In process
The work item is currently being processed by a recipient or in a different mode.
A work item also has this status
 if the work item is waiting for its terminating event.
 if the user cancelled the method.
 if the method was ended with a temporary exception for which no
subsequent steps have been modeled.
The point at which processing is completed cannot be detected by the workflow system in this status. As long as the status of the work item is set to in process, database changes have not been made.

Executed
The work item is awaiting explicit confirmation of its completion.
The work item only has this status if it is necessary to confirm that it has been completed. A work item with executed status can be executed or forwarded several times until it is set to the status done in the Business Workplace.
In this way, groupware components are realized in SAP Business Workflow.

completed
The execution of the work item is completed.
The result of the task represented by the work item is correct, i.e. the result modeled in the workflow definition.
Work items in the completed status are not displayed in the workflow inbox of the Business Workplace.

Logically deleted
Execution of the work item is no longer meaningful or required by the workflow logic.
A work item changes to the logically deleted status in the following way:
 Termination in parallel processing branches
When the required number of processing paths has been executed in a fork, the work items in the other paths that have not yet reached the completed status are automatically set to the logically deleted status.
 Intervention by an administrator
An administrator can only set a work item to the logically deleted status if it has not yet reached the completed status and is not part of a higher-level workflow.
Work items in the logically deleted status are not displayed in the workflow inbox of the Business Workplace.
 A work item with the logically deleted status may have caused database changes or other actions (generate event, send notification). These changes are not canceled automatically
(compensated).

error
Execution of the work item was terminated with an error.

posted @ 2004-07-23 00:41 温少 阅读(1735) 评论(1) 编辑