java - Web 方法 :can we remove an element from the documentList while looping through it

标签 java jakarta-ee webmethods

在 WEBMETHODS 中,可以在循环遍历 DocumentList 时从 DocumentList 中删除元素吗?如果是那么怎么办?如果不是,那么我们如何将 DocumentList 变量的值设置为 null。

谢谢
nohsib

最佳答案

Tundra包括从文档列表中删除项目的服务 (com.wm.da​​ta.IData[]):tundra.list.document:drop($list[], $index).

  • $list 是您要从中删除项目的文档列表 (com.wm.da​​ta.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 )。

关于java - Web 方法 :can we remove an element from the documentList while looping through it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9811770/

相关文章:

java - JMSTemplate send() 回滚或超时

java - <瓷砖:insertTemplate> is slow

java - 在 Linux 上的 java hotspot jvm 中使用大页面和 DirectByteBuffer

java - 使用 struts.xml 中的 Contants 作为注解值 - Struts2 Java

java - 使用 openSession() 而不是 getCurrentSession() - 何时以及为什么?

java - 是否需要文件 persistence.xml?

mysql - JDBC 连接失败,当第一次尝试登录时,过了一会儿

java - GlassFish 上 Quartz 计划的 JSR-352 作业无法启动

java - 如何为具有 Webmethod 模式命名空间的 xml 创建 xsd

integration - Web 方法包删除恢复