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


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