在 WEBMETHODS 中,可以在循环遍历 DocumentList 时从 DocumentList 中删除元素吗?如果是,那么如何?如果否,那么我们如何将 DocumentList 变量的值设置为 null。
感谢
nohsib
在 WEBMETHODS 中,可以在循环遍历 DocumentList 时从 DocumentList 中删除元素吗?如果是,那么如何?如果否,那么我们如何将 DocumentList 变量的值设置为 null。
感谢
nohsib
Tundra包含一项用于从文档列表 ( com.wm.data.IData[]
) 中删除项目的服务:tundra.list.document:drop($list[], $index)
.
$list
com.wm.data.IData[]
是您要从中删除项目的文档列表 ( )$index
是要删除的项目的从零开始的数组索引相关Java代码如下:
public static final void drop(IData pipeline) throws ServiceException {
IDataCursor cursor = pipeline.getCursor();
try {
Object[] list = IDataUtil.getObjectArray(cursor, "$list");
String index = IDataUtil.getString(cursor, "$index");
if (index != null) IDataUtil.put(cursor, "$list", drop(list, index));
} finally {
cursor.destroy();
}
}
// returns a new array which contains all the elements from the given arrays
public static <T> T[] concatenate(T[] array, T[] items) {
if (array == null) return items;
if (items == null) return array;
java.util.List<T> list = new java.util.ArrayList<T>(array.length + items.length);
java.util.Collections.addAll(list, array);
java.util.Collections.addAll(list, items);
return list.toArray(java.util.Arrays.copyOf(array, 0));
}
// removes the element at the given index from the given list
public static <T> T[] drop(T[] array, String index) {
return drop(array, Integer.parseInt(index));
}
// removes the element at the given index from the given list
public static <T> T[] drop(T[] array, int index) {
if (array != null) {
// support reverse/tail indexing
if (index < 0) index += array.length;
if (index < 0 || array.length <= index) throw new ArrayIndexOutOfBoundsException(index);
T[] head = slice(array, 0, index);
T[] tail = slice(array, index + 1, array.length - index);
array = concatenate(head, tail);
}
return array;
}
// returns a new array which is a subset of elements from the given array
public static <T> T[] slice(T[] array, int index, int length) {
if (array == null || array.length == 0) return array;
// support reverse/tail length
if (length < 0) length = array.length + length;
// support reverse/tail indexing
if (index < 0) index += array.length;
// don't slice past the end of the array
if ((length += index) > array.length) length = array.length;
return java.util.Arrays.copyOfRange(array, index, length);
}
但是,我同意MrJames的观点:最好和最简单的方法是创建一个新的文档列表,并且只在循环步骤中使用pub.list:appendToDocumentList
(或者tundra.list.document:append
,如果您使用Tundra )将您想要的项目附加到新列表中。
您可以为此使用内置服务的文档删除文档。
这是一个非常简单的例子,希望这能反映函数的用法。
希望这可以帮助。
我进行了一些测试,可以将特定的文档项设置为空(使用服务 pub.list:setListItem),但文档列表将保持相同的大小。
另一种方法是循环文档列表并将您感兴趣的文档附加到新文档列表(pub.list:appendToDocumentList)。
关于如何将变量设置为null的另一个问题,您可以使用管道上的Drop
PS:使用 webMethods 7.1.2