当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java CompositeName remove()用法及代码示例


javax.naming.CompositeName类的remove()方法用于从此组合名称中移除位于位置posn的组件。位置posn作为参数传递给此方法。此方法将删除此复合名称对象在“ posn”位置的组成部分,并将位于“ posn”以上的位置的组成部分向下移动一位。对象的大小减小1。

用法:

public Object remove(int posn)
       throws InvalidNameException

参数:此方法接受posn,这是要删除的组件的索引。必须在[0,size())范围内。



返回值:此方法返回删除的组件(字符串)。

异常:如果posn超出指定范围(包括复合名称为空的情况),则此方法引发ArrayIndexOutOfBoundsException;如果删除组件将违反复合名称的语法,则抛出InvalidNameException。

下面的程序说明CompositeName.remove()方法:
程序1:

// 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

程序2:

// 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)




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 CompositeName remove() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。