DECLARE
TYPE numlist IS TABLE OF NUMBER;
n NUMLIST := Numlist(1,3,5,7);
counter INTEGER;
BEGIN
dbms_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. We start at the first element, and
-- keep looking 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;
/
Using FIRST and LAST With a Collection
0 comments:
Post a Comment