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


Java CompositeName add()用法及代码示例


javax.naming.CompositeName类的add()方法用于将组件添加到CompositeName对象。有两种不同的添加方法。

  1. add(int,字符串):此方法用于在此复合名称内作为参数传递的指定位置posn处添加单个组件。在新组件的位置或之后出现的此复合名称对象的其他组件将上移一个位置以容纳新组件。

    用法:

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

    参数:此方法接受:



    • posn这是添加新组件的索引,并且
    • comp这是要添加到复合名称对象中的新组件。

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

    异常:如果posn在指定范围之外,则此方法引发ArrayIndexOutOfBoundsException;如果在指定位置添加comp将违反复合名称的语法,则抛出InvalidNameException。

    下面的程序说明CompositeName.add()方法:
    程序1:

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

    程序2:

    // Java program to demonstrate 
    // CompositeName.add() 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( 
                    "c/e/d/v/a/b/z/y/x/f"); 
      
            // apply add() to add new component at posn 9 
            CompositeName newCompositeName 
                = (CompositeName) 
                      CompositeName1.add(9, "zzzzz"); 
      
            // print value 
            System.out.println( 
                "Updated CompositeName Object:"
                + newCompositeName); 
        } 
    }
    输出:
    Updated CompositeName Object:c/e/d/v/a/b/z/y/x/zzzzz/f
    
  2. 添加(字符串):Thie方法用于在此组合名称的末尾添加单个组件。
    用法:
    public Name add(String comp)
           throws InvalidNameException
    

    参数:此方法接受comp,这是要在末尾添加复合名称对象的新组件。

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

    异常:如果在名称末尾添加comp会违反组合名称的语法,则此方法将引发InvalidNameException。



    下面的程序说明CompositeName.add()方法:
    程序1:

    // Java program to demonstrate 
    // CompositeName.add() 
      
    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/c/d/e/f/g/h/i/j"); 
      
            // apply add() to add new component at end 
            CompositeName newCompositeName 
                = (CompositeName) 
                      CompositeName1.add("k"); 
      
            // print value 
            System.out.println( 
                "Updated CompositeName Object:"
                + newCompositeName); 
        } 
    }
    输出:
    Updated CompositeName Object:a/b/c/d/e/f/g/h/i/j/k
    

    程序2:

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

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




相关用法


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