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


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


javax.naming.CompoundName類的addAll()方法用於將不同化合物名稱對象的所有組件添加到此CompoundName對象。有兩種不同的addAll()方法。

  1. addAll(int,名稱):此方法用於添加另一個複合名稱對象的所有組件,該複合名稱作為參數傳遞到此複合名稱對象中指定位置posn。在第一個新組件的位置或之後出現的此複合名稱對象的其他組件將上移以容納不同組件的所有組件。

    用法:

    public Name addAll(int posn, Name n)
           throws InvalidNameException
    

    參數:此方法接受:



    • posn這是添加所有新組件的索引,並且
    • n這是要添加的非null組件。

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

    異常:如果posn不在指定範圍內,則此方法引發ArrayIndexOutOfBoundsException;如果n不是複合名稱,或者組件的添加違反了此複合名稱的語法(例如超過組件數目),則拋出InvalidNameException。

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

    // Java program to demonstrate 
    // CompoundName.addAll() 
      
    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); 
      
            // different component to add at position 0 
            CompoundName diffrenetComponent 
                = new CompoundName( 
                    "9@99@999@9999", 
                    props); 
      
            // apply addAll() to add All 
            // new components at posn 0 
            CompoundName newCompoundName 
                = (CompoundName) 
                      CompoundName1.addAll( 
                          0, diffrenetComponent); 
      
            // print value 
            System.out.println( 
                "Updated CompoundName Object:"
                + newCompoundName); 
        } 
    }
    輸出:
    Updated CompoundName Object:9@99@999@9999@1@2@3@4@5@6@7
    
  2. addAll(String):Thie方法用於將經過輸入的不同複合名稱對象的所有組件添加到該複合名稱的末尾。
    用法:
    public Name addAll(Name suffix)
           throws InvalidNameException
    

    參數:此方法接受後綴,該後綴是要添加的非null組件。

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

    異常:如果後綴不是複合名稱,或者添加的組件違反了此複合名稱的語法(例如,超出組件數量),則此方法將引發InvalidNameException。

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

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

參考文獻:
https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#addAll(javax.naming.Name)
https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#addAll(int, javax.naming.Name)




相關用法


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