當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。