集合与容器

From Apache OpenOffice Wiki
Jump to: navigation, search




集合容器的概念适用于包含多个子对象、子对象的数目通常无法预先确定的对象。对于术语集合,子对象由集合本身隐式确定,而对于术语容器,则可以显式加入新的子对象和删除现有子对象。这样,容器将 insert()remove() 等方法添加到集合接口。

一般来说,OpenOffice.org API 集合和容器接口包含可以用 UNO 类型 any 表示的任何类型。但是,可以将许多容器实例绑定到某个特定类型或此类型的子类型。这是一个运行时和规范协议,而且无法在运行时进行检查。

集合的基接口是 com.sun.star.container.XElementAccess,此接口确定子对象的类型(如果子对象类型是由集合确定的)以及包含的子对象数目。有三种主要的集合接口类型是基于 XElementAccess 的:


com.sun.star.container.XIndexAccess 用于按索引替换现有子对象,而 com.sun.star.container.XIndexContainer 用于插入和删除子对象。您可以看到 com.sun.star.container.XNameAccess 与其他特定集合类型具有同样的类似属性。


所有容器都支持 com.sun.star.container.XContainer,它具有注册 com.sun.star.container.XContainerListener 接口的接口。这样,应用程序就可以了解容器中插入和删除子对象的情况。

Documentation note.png The com.sun.star.container.XIndexAccess 对程序员很有吸引力,因为它通常非常容易实现。但只有在集合真正具有索引的情况下,才应该实现此接口。


有关集合和容器接口的详细信息,请参阅 API 引用中的 <idlmoduel>com.sun.star.container</idlmodule> 模块。


以下示例说明三个主要集合接口的用法。首先,对具有索引的集合进行遍历。索引始终从 0 开始,并且是连续的:

  // get an XIndexAccess interface from the collection
  XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
      XIndexAccess.class, mxCollection);
 
  // iterate through the collection by index
  int i;
  for (i = 0; i < xIndexAccess.getCount(); ++i) {
      Object aSheet = xIndexAccess.getByIndex(i);
      Named xSheetNamed = (XNamed) oRuntime.queryInterface(XNamed.class, aSheet);
      System.out.println("sheet #" + i + " is named '" + xSheetNamed.getName() + "'");
  }


接下来的示例按命名对象对集合进行遍历。元素名称在集合内是唯一的,并且区分大小写。

  // get an XNameAccess interface from the collection
  XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, mxCollection);
 
  // get the list of names
  String[] aNames = xNameAccess.getElementNames();
 
  // iterate through the collection by name
  int i;
  for (i = 0; i < aNames.length; ++i) {
      // get the i-th object as a UNO Any
      Object aSheet = xNameAccess.getByName( aNames[i] );
 
      // get the name of the sheet from its XNamed interface
      XNamed xSheetNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, aSheet);
      System.out.println("sheet '" + aNames[i] + "' is #" + i);
  }

下一个示例说明如何使用枚举器对集合进行遍历。未定义枚举顺序。只有在枚举所有元素的情况下才定义枚举顺序。如果在创建枚举器后修改了集合,则不定义行为。

  // get an XEnumerationAccess interface from the collection
  XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(
      XEnumerationAccess.class, mxCollection );
 
  // create an enumerator
  XEnumeration xEnum = xEnumerationAccess.createEnumeration();
 
  // iterate through the collection by name
  while (xEnum.hasMoreElements()) {
      // get the next element as a UNO Any
      Object aSheet = xEnum.nextElement();
 
      // get the name of the sheet from its XNamed interface
      XNamed xSheetNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, aSheet);
      System.out.println("sheet '" + xSheetNamed.getName() + "'");
  }


有关说明容器用法的示例,请参阅 文本文档 - 全局文档特性 - 样式,其中,一个新样式被添加到样式系列 ParagraphStyles 中。


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages