《Oracle PL/SQL开发指南》学习笔记29——源码调试——Oracle集合API(第七部分)

本文深入探讨了Oracle Database 12C中集合API的使用方法,包括COUNT、DELETE、EXISTS、EXTEND等方法的详细解释与示例,展示了如何在PL/SQL中有效操作集合数据,提升代码调试与问题解决能力。

在调试代码的过程中,才发现很多代码没法直接执行成功,需要做些修改方可成功执行。

不知道是作者有意为之,还是疏忽造成的呢?我宁愿相信是前者。通过不断的代码调试,可以提高我们解决实际问题的能力!

1. Oracle Database 12C的集合API总结

Method方法Definition
COUNT  元素个数Returns the number of elements that have been allocated space in varray and table  data types, and returns the total of all elements in associative arrays. The return value  of the COUNT method can be smaller than the return value of LIMIT for the varray  collection.  It has the following prototype:  PLS_INTEGER COUNT
DELETE删除元素lets you remove elements from table collections and associative arrays. It doesn’t  work with varray collections, and an attempt to remove an element from a varray  raises a PLS-00306 exception.  The DELETE method takes two formal parameters; one is mandatory and the other is  optional. Both parameters are index values and must occur in ascending order. The  DELETE procedure deletes everything from the parameter n to m, inclusively, when  you supply two parameters.  The prototypes are as follows:  void DELETE(n)  void DELETE(n,m)  
EXISTS     指定元素是否存在Checks to find an element with the supplied index in a collection. It returns true when  the element is found and null when an initialized varray or table is empty. It has one  mandatory parameter, which is a valid index value.  It has the following prototype:  Boolean EXISTS(n)
EXTEND    分配空间Allocates space for one or more new elements in a varray or table collection. It has two  optional parameters. It adds space for one element by default without a parameter.  The first parameter designates how many physical spaces should be allocated in  memory, the only constraint being the limit (or maximum value) of the varray.  The second parameter is an index value. When the function receives two parameters,  the first determines how many elements to add and the second is an index value. The  EXTEND procedure uses the index value to copy a value into the newly added space.  It has the following prototypes:  void EXTEND  void EXTEND(n)  void EXTEND(n,i)
FIRST最小下标Returns the lowest subscript value in a collection. It can return a  PLS_INTEGER, VARCHAR2, or LONG type.  It has the following prototype:  mixed FIRST
LAST最大下标Returns the highest subscript value in a collection. It can return a  PLS_INTEGER, VARCHAR2, or LONG type.  It has the following prototype:  mixed LAST
LIMIT变长数组最大长度(最大元素个数)Returns the highest possible subscript value in a collection. It can only return a  PLS_INTEGER type, and can only be used by a VARRAY data type.  It has the following prototype:  mixed LIMIT  
NEXT(n)下一下标Returns the next higher subscript value in a collection when successful; otherwise  returns false. The return value is a PLS_INTEGER, VARCHAR2, or LONG type. It requires  a valid index value as an actual parameter and raises an exception when the index is  invalid.  It has the following prototype:  mixed NEXT(n)  
PRIOR(n)上一下标Returns the next lower subscript value in a collection when successful; otherwise  returns false. The return value is a PLS_INTEGER, VARCHAR2, or LONG type. It requires  a valid index value as an actual parameter and raises an exception when the index is  invalid.  It has the following prototype:  mixed PRIOR(n)  
TRIM   移除下标(从集合末尾删除元素)Removes a subscripted value from a collection. It has one optional parameter. Without  an actual parameter, it removes the highest element from the array. An actual parameter  is interpreted as the number of elements removed from the end of the collection.  It has the following prototype:  void TRIM  void TRIM(n)

2.  EXISTS方法

1)EXISTS方法的局限性

SQL> ed
已写入 file afiedt.buf

  1  DECLARE
  2    /* Define the table collection */
  3    TYPE empty_table IS TABLE OF prominent_object;
  4    /* Declare a table collection variable */
  5    lv_array EMPTY_TABLE := empty_table(null);
  6  BEGIN
  7    /* Check whether the element is allocate in memory. */
  8    IF lv_array.EXISTS(1) THEN
  9      dbms_output.put_line('Valid collection');
 10    ELSE
 11     dbms_output.put_line('Invalid collection');
 12    END IF;
 13* END;
SQL> /
Valid collection

PL/SQL 过程已成功完成。

2 )EXISTS最佳使用方法

SQL> ED
已写入 file afiedt.buf

  1  DECLARE
  2    /* Define table. */
  3    TYPE x_table IS TABLE OF VARCHAR2(10);
  4    /* Declare an index counter. */
  5    lv_index  NUMBER := 1;
  6    /* Declare a local collection variable. */
  7    lv_table  X_TABLE := x_table();
  8  BEGIN
  9    IF lv_table.EXISTS(lv_index) AND NOT lv_table.COUNT = 0 AND lv_table(lv_index) IS NOT NULL THEN
 10      dbms_output.put_line(lv_table(lv_index));
 11    ELSIF lv_table.EXISTS(lv_index) AND NOT lv_table.COUNT = 0 AND lv_table IS NOT EMPTY THEN
 12      dbms_output.put_line('lv_table('||lv_index||') is NOT EMPTY?');
 13    ELSE
 14      dbms_output.put_line('lv_table('||lv_index||') is EMPTY? NULL? NOT EMPTY? NOT NULL?');
 15    END IF;
 16    --dbms_output.put_line(lv_table IS EMPTY);
 17    dbms_output.put_line('lv_table COUNT? '|| lv_table.COUNT);
 18* END;
SQL> /
lv_table(1) is EMPTY? NULL? NOT EMPTY? NOT NULL?
lv_table COUNT? 0

PL/SQL 过程已成功完成。

 

SQL> ED
已写入 file afiedt.buf

  1  DECLARE
  2    /* Define table. */
  3    TYPE x_table IS TABLE OF VARCHAR2(10);
  4    /* Declare an index counter. */
  5    lv_index  NUMBER := 1;
  6    /* Declare a local collection variable. */
  7    lv_table  X_TABLE := x_table('Something');
  8  BEGIN
  9    IF lv_table.EXISTS(lv_index) AND lv_table.COUNT > 0 THEN
 10      dbms_output.put_line(lv_table(lv_index));
 11    END IF;
 12* END;
 13  /
Something

PL/SQL 过程已成功完成。

3. COUNT方法

SQL> ed
已写入 file afiedt.buf

  1  DECLARE
  2    /* Define a table collection. */
  3    TYPE NUMBER_table IS TABLE OF INTEGER;
  4    /* Declare an initialized table collection. */
  5    lv_table  NUMBER_TABLE := number_table(1,2,3,4,5);
  6  BEGIN
  7    DBMS_OUTPUT.PUT_LINE('How many? ['||lv_table.COUNT||']');
  8* END;
  9  /
How many? [5]

PL/SQL 过程已成功完成。

4. DELETE方法

SQL> ed
已写入 file afiedt.buf

  1  DECLARE
  2    /* Declare variable with meaningful name. */
  3    current INTEGER;
  4    /* Define a table collection. */
  5    TYPE x_table IS TABLE OF VARCHAR2(6);
  6    /* Declare an initialized table collection. */
  7    lv_table  X_TABLE := x_table('One','Two','Three','Four','Five');
  8  BEGIN
  9    /* Remove one element with an index of 2. */
 10    lv_table.DELETE(2,2);
 11    /* Remove elements for an inclusive range of 4 to 5. */
 12    lv_table.DELETE(4,5);
 13    /* Set the starting index. */
 14    current := lv_table.FIRST;
 15    /* Read through index values in ascending order. */
 16    WHILE (current <= lv_table.LAST) LOOP
 17      dbms_output.put_line(
 18        'Index ['||current||'] Value ['||lv_table(current)||']');
 19      /* Shift index to next higher value. */
 20      current := lv_table.NEXT(current);
 21    END LOOP;
 22* END;
 23  /
Index [1] Value [One]
Index [3] Value [Three]

PL/SQL 过程已成功完成。

5. EXTEND方法

SQL> DECLARE
  2    /* Declare variable with meaningful name. */
  3    current INTEGER;
  4
  5    /* Define a table collection. */
  6    TYPE x_table IS TABLE OF VARCHAR2(6);
  7
  8    /* Declare an initialized table collection. */
  9    lv_table  X_TABLE := x_table('One');
 10  BEGIN
 11    /* Extend space, and assign a value. */
 12    lv_table.EXTEND;
 13
 14    /* Assign a value to the last allocated element. */
 15    lv_table(lv_table.COUNT) := 'Two';
 16
 17    /* Set the starting index. */
 18    current := lv_table.FIRST;
 19
 20    /* Read through index values in ascending order. */
 21    WHILE (current <= lv_table.LAST) LOOP
 22      dbms_output.put_line(
 23        'Index ['||current||'] Value ['||lv_table(current)||']');
 24      /* Shift index to next higher value. */
 25      current := lv_table.NEXT(current);
 26    END LOOP;
 27  END;
 28  /
Index [1] Value [One]
Index [2] Value [Two]

PL/SQL 过程已成功完成。

 

 

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值