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


Java CompoundName add()用法及代碼示例

javax.naming.CompoundName類的add()方法用於將組件添加到CompoundName對象。有兩種不同的添加方法。

  1. add(int,字符串):此方法用於在此複合名稱中作為參數傳遞的指定位置posn處添加單個組件。在新組件的位置或之後出現的此複合名稱對象的其他組件將上移一個位置以容納新組件。

    用法:

    public Name add(int posn, String comp)
             throws InvalidNameException
    

    參數:此方法接受:



    • posn這是添加新組件的索引,並且
    • comp這是要添加到複合名稱對象中的新組件。

    返回值:此方法返回更新的compoundName,而不是新的。返回的值不能為空。

    異常:如果posn在指定範圍之外,則此方法將引發ArrayIndexOutOfBoundsException;如果在指定位置添加comp將違反複合名稱的語法,則將引發InvalidNameException。

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

    // Java program to demonstrate 
    // CompoundName.add() 
      
    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( 
                    "1@2@3@4@5@6@7", 
                    props); 
      
            // apply add() to add 
            // new component at posn 0 
            CompoundName newCompoundName 
                = (CompoundName) 
                      CompoundName1.add(0, "000"); 
      
            // print value 
            System.out.println( 
                "Updated CompoundName Object:"
                + newCompoundName); 
        } 
    }
    輸出:
    Updated CompoundName Object:000@1@2@3@4@5@6@7
    

    程序2:

    // Java program to demonstrate 
    // CompoundName.add() 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 add() to add 
            // new component at posn 9 
            CompoundName newCompoundName 
                = (CompoundName) 
                      CompoundName1.add( 
                          9, "zzzzz"); 
      
            // print value 
            System.out.println( 
                "Updated CompoundName Object:"
                + newCompoundName); 
        } 
    }
    輸出:
    Updated CompoundName Object:c/e/d/v/a/b/z/y/x/zzzzz/f
    
  2. 添加(字符串):Thie方法用於在此化合物名稱的末尾添加單個成分。
    用法:
    public Name add(String comp)
           throws InvalidNameException
    

    參數:此方法接受comp,這是要在末尾添加化合物名稱對象的新組件。

    返回值:此方法返回更新的compoundName,而不是新的。返回的值不能為空。

    異常:如果在名稱末尾添加comp會違反複合名稱的語法,則此方法將引發InvalidNameException。



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

    // Java program to demonstrate 
    // CompoundName.add() 
      
    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( 
                    "1@2@3@4@5@6@7", 
                    props); 
      
            // apply add() to add 
            // new component at end 
            CompoundName newCompoundName 
                = (CompoundName) 
                      CompoundName1.add("9"); 
      
            // print value 
            System.out.println( 
                "Updated CompoundName Object:"
                + newCompoundName); 
        } 
    }
    輸出:
    Updated CompoundName Object:1@2@3@4@5@6@7@9
    

    程序2:

    // Java program to demonstrate 
    // CompoundName.add() 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 add() to add 
            // new component at end 
            CompoundName newCompoundName 
                = (CompoundName) 
                      CompoundName1.add("ppp"); 
      
            // print value 
            System.out.println( 
                "Updated CompoundName Object:"
                + newCompoundName); 
        } 
    }
    輸出:
    Updated CompoundName Object:c/e/d/v/a/b/z/y/x/f/ppp
    

參考文獻:
https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#add(int, java.lang.String)
https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#add(java.lang.String)




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 CompoundName add() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。