Java中的 CompositeName remove() 方法及示例
javax.naming.CompositeName 类的remove()方法用于从该复合名称中删除位于位置posn的组件。位置posn作为参数传递给此方法。此方法删除此复合名称对象在位置 'posn' 处的组件,并且在大于 'posn' 的位置处存在的组件向下移动 1。对象的大小减 1。
句法:
public Object remove(int posn)
throws InvalidNameException
参数:此方法接受posn ,它是要删除的组件的索引。必须在 [0, size()) 范围内。
返回值:此方法返回已移除的组件(一个字符串)。
异常:此方法抛出ArrayIndexOutOfBoundsException如果 posn 超出指定范围(包括复合名称为空的情况)和 InvalidNameException 如果删除组件将违反复合名称的语法。
下面的程序说明了 CompositeName.remove() 方法:
方案一:
// Java program to demonstrate
// CompositeName.remove()
import java.util.Properties;
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
// create composite name object
CompositeName CompositeName1
= new CompositeName(
"a/b/d/e/g/h/j/k/l");
// apply remove()
String removedComponent
= (String)
CompositeName1.remove(5);
// print value
System.out.println(
"Removed Component: "
+ removedComponent);
System.out.println(
"CompositeName After removal: "
+ CompositeName1);
}
}
输出:
Removed Component: h
CompositeName After removal: a/b/d/e/g/j/k/l
方案二:
// Java program to demonstrate
// CompositeName.remove() method
import java.util.Properties;
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
public class GFG {
public static void main(String[] args)
throws InvalidNameException
{
// create composite name object
CompositeName CompositeName1
= new CompositeName(
"aa/bb/cc/dd/ee/ff/gg/hh");
// apply remove()
String removedComponent1
= (String)
CompositeName1.remove(1);
// print value
System.out.println("Removed Component: "
+ removedComponent1);
System.out.println("CompositeName After removal: "
+ CompositeName1);
// again remove component
String removedComponent2
= (String)
CompositeName1.remove(2);
// print results
System.out.println(
"Removed Component: "
+ removedComponent2);
System.out.println(
"CompositeName After removal: "
+ CompositeName1);
}
}
输出:
Removed Component: bb
CompositeName After removal: aa/cc/dd/ee/ff/gg/hh
Removed Component: dd
CompositeName After removal: aa/cc/ee/ff/gg/hh
参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompositeName.html#remove(int)