示例2:演示 collection 中元素的个数
DECLARETYPE NumList IS TABLE OF NUMBER;
n NumList := NumList(2,4,6,8);
-- Collection starts with 4 elements.BEGINDBMS_OUTPUT.PUT_LINE
('There are ' || n.COUNT || ' elements in N.');
n.EXTEND(3); -- Add 3 new elements at the end.DBMS_OUTPUT.PUT_LINE
('Now there are ' || n.COUNT || ' elements in N.');
n := NumList(86,99); -- Assign a completely new value with 2 elements.DBMS_OUTPUT.PUT_LINE
('Now there are ' || n.COUNT || ' elements in N.');
n.TRIM(2); -- Remove the last 2 elements, leaving none.
DBMS_OUTPUT.PUT_LINE
('Now there are ' || n.COUNT || ' elements in N.');
END;/
LIMIT 方法
LIMIT 返回 collection 可以容纳元素的最大数量。若 collection 没有最大大小,则 LIMIT 回返 NULL。
示例3:演示检查 collection 的最大容量
DECLARETYPE dnames_var IS VARRAY(7) OF VARCHAR2(30);
dept_names dnames_var :=
dnames_var('Shipping','Sales','Finance','Payroll');
BEGINDBMS_OUTPUT.PUT_LINE
('dept_names has ' || dept_names.COUNT || ' elements now');
DBMS_OUTPUT.PUT_LINE
('dept_names''s type can hold a maximum of '
|| dept_names.LIMIT || ' elements');
DBMS_OUTPUT.PUT_LINE
('The maximum number you can use with ' || 'dept_names.EXTEND() is '|| (dept_names.LIMIT - dept_names.COUNT));
END;/
FIRST 和 LAST 方法
FIRST 方法和 LAST 方法返回的值,取决于 collection 的索引类型,是 integer,还是 string。
对于使用 integer 索引的 collection,FIRST 方法和 LAST 方法返回第一个和最后一个(最小和最大)的索引数。
对于使用 string 索引的 associative array,FIRST 方法和 LAST 方法返回最前和最后的键值。若 NLS_COMP 初始化参数设置为 ANSI,顺序是基于指定的 NLS_SORT 初始化参数。
若 collection 是空的,则 FIRST 和 LAST 返回 NULL。若 collection 只包含一个元素,则 FIRST 和 LAST 返回相同的值。
示例4:演示使用 FIRST 和 LAST 方法
DECLARETYPE NumList IS TABLE OF NUMBER;
n NumList := NumList(1,3,5,7);
counter INTEGER;BEGINDBMS_OUTPUT.PUT_LINE('N''s first subscript is ' || n.FIRST);
DBMS_OUTPUT.PUT_LINE('N''s last subscript is ' || n.LAST);
-- When the subscripts are consecutive starting at 1, -- it's simple to loop through them.FOR i IN n.FIRST .. n.LAST
LOOP
DBMS_OUTPUT.PUT_LINE('Element #' || i || ' = ' || n(i));
END LOOP;n.DELETE(2); -- Delete second element.
-- When the subscripts have gaps-- or the collection might be uninitialized,-- the loop logic is more extensive.-- Start at the first element-- and look for the next element until there are no more.IF n IS NOT NULL THEN
counter := n.FIRST;WHILE counter IS NOT NULL
LOOP
DBMS_OUTPUT.PUT_LINE
('Element #' || counter || ' = ' || n(counter));
counter := n.NEXT(counter); END LOOP; ELSE DBMS_OUTPUT.PUT_LINE('N is null, nothing to do.');END IF;
END;/
