示例7:演示增加 collection 的大小
DECLARETYPE NumList IS TABLE OF INTEGER;
n NumList := NumList(2,4,6,8);
x NumList := NumList(1,3);
PROCEDURE print_numlist(the_list NumList) IS
output VARCHAR2(128); BEGINFOR i IN the_list.FIRST .. the_list.LAST
LOOP
output :=output || NVL(TO_CHAR(the_list(i)),'NULL') || ' ';
END LOOP; DBMS_OUTPUT.PUT_LINE(output); END;BEGINDBMS_OUTPUT.PUT_LINE
('At first, N has ' || n.COUNT || ' elements.');
n.EXTEND(5); -- Add 5 elements at the end.DBMS_OUTPUT.PUT_LINE
('Now N has ' || n.COUNT || ' elements.');
-- Elements 5, 6, 7, 8, and 9 are all NULL.print_numlist(n);
DBMS_OUTPUT.PUT_LINE
('At first, X has ' || x.COUNT || ' elements.');
x.EXTEND(4,2); -- Add 4 elements at the end.DBMS_OUTPUT.PUT_LINE
('Now X has ' || x.COUNT || ' elements.');
-- Elements 3, 4, 5, and 6 are copies of element #2.print_numlist(x);
END;/

备注:
若初始化一个有 5 个元素的 nested table,之后,删除元素 2 和 5,内部大小为 5,COUNT 方法返回 3,LAST 方法返回 4。所有已删除的元素,不管其位置如何,都同等对待。
TRIM 方法
该方法有如下两个形式:
- TRIM 从 collection 末尾移除一个元素。
- TRIM(n) 从 collection 末尾移除 n 个元素。
若想移除所有元素,使用无参的 DELETE 方法。
不能在 associative array 上使用 TRIM 方法。
示例8:演示使用 TRIM 减少 collection 的大小
DECLARETYPE NumList IS TABLE OF NUMBER;
n NumList := NumList(1,2,3,5,7,11);
PROCEDURE print_numlist(the_list NumList) IS
output VARCHAR2(128); BEGINIF n.COUNT = 0 THEN
DBMS_OUTPUT.PUT_LINE('No elements in collection.'); ELSEFOR i IN the_list.FIRST .. the_list.LAST
LOOP
output :=output || NVL(TO_CHAR(the_list(i)),'NULL') || ' ';
END LOOP; DBMS_OUTPUT.PUT_LINE(output);END IF;
END;BEGINprint_numlist(n);
n.TRIM(2); -- Remove last 2 elements.
print_numlist(n);
n.TRIM; -- Remove last element.
print_numlist(n);
n.TRIM(n.COUNT); -- Remove all remaining elements.
print_numlist(n);
-- If too many elements are specified, -- TRIM raises the exception SUBSCRIPT_BEYOND_COUNT. BEGINn := NumList(1,2,3);
n.TRIM(100); EXCEPTIONWHEN SUBSCRIPT_BEYOND_COUNT THEN
DBMS_OUTPUT.PUT_LINE
('There weren''t 100 elements to be trimmed.');
END;-- When elements are removed by DELETE,-- placeholders are left behind.-- TRIM counts these placeholders-- as it removes elements from the end.n := NumList(1,2,3,4);
n.DELETE(3); -- delete element 3
-- At this point, n contains elements (1,2,4).-- TRIMming the last 2 elements-- removes the 4 and the placeholder, not 4 and 2. n.TRIM(2);print_numlist(n);
END;/

备注:
若 n 太大,则会产生 SUBSCRIPT_BEYOND_COUNT 异常。
