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


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


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

用法:

public Object remove(int posn)
       throws InvalidNameException

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



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

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

以下示例程序旨在说明CompoundName.remove()方法:
程序1:

// Java program to demonstrate 
// CompoundName.remove() 
import java.util.Properties; 
  
import javax.naming.CompoundName; 
import javax.naming.InvalidNameException; 
  
public class GFG { 
    public static void main(String[] args) throws InvalidNameException 
    { 
  
        // need properties for CompoundName 
        Properties props = new Properties(); 
        props.put("jndi.syntax.separator", "@"); 
        props.put("jndi.syntax.direction", 
                  "left_to_right"); 
  
        // create compound name object 
        CompoundName CompoundName1 
            = new CompoundName( 
                "0@1@2@3@4@5@6@7", 
                props); 
  
        // apply remove() 
        String removedComponent 
            = (String) 
                  CompoundName1.remove(5); 
  
        // print value 
        System.out.println( 
            "Removed Component:"
            + removedComponent); 
        System.out.println( 
            "CompoundName After removal:"
            + CompoundName1); 
    } 
}
输出:
Removed Component:5
CompoundName After removal:0@1@2@3@4@6@7

程序2:

// Java program to demonstrate 
// CompoundName.remove() method 
import java.util.Properties; 
  
import javax.naming.CompoundName; 
import javax.naming.InvalidNameException; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InvalidNameException 
    { 
  
        // need properties for CompoundName 
        Properties props = new Properties(); 
        props.put("jndi.syntax.separator", "/"); 
        props.put("jndi.syntax.direction", 
                  "left_to_right"); 
  
        // create compound name object 
        CompoundName CompoundName1 
            = new CompoundName( 
                "c/e/d/v/a/b/z/y/x/f", props); 
  
        // apply remove() 
        String removedComponent1 
            = (String) 
                  CompoundName1.remove(1); 
  
        // print value 
        System.out.println( 
            "Removed Component:"
            + removedComponent1); 
        System.out.println( 
            "CompoundName After removal:"
            + CompoundName1); 
  
        // again remove component 
        String removedComponent2 
            = (String) 
                  CompoundName1.remove(2); 
  
        // print results 
        System.out.println("Removed Component:"
                           + removedComponent2); 
        System.out.println("CompoundName After removal:"
                           + CompoundName1); 
    } 
}
输出:
Removed Component:e
CompoundName After removal:c/d/v/a/b/z/y/x/f
Removed Component:v
CompoundName After removal:c/d/a/b/z/y/x/f

参考文献:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#remove(int)




相关用法


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