javax.naming.CompoundName类的add()方法用于将组件添加到CompoundName对象。有两种不同的添加方法。
- 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
- 添加(字符串):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)
相关用法
- Java CompoundName get()用法及代码示例
- Java CompoundName isEmpty()用法及代码示例
- Java CompoundName size()用法及代码示例
- Java CompoundName getAll()用法及代码示例
- Java CompoundName remove()用法及代码示例
- Java CompoundName endsWith()用法及代码示例
- Java CompoundName compareTo()用法及代码示例
- Java CompoundName clone()用法及代码示例
- Java CompoundName hashCode()用法及代码示例
- Java CompoundName addAll()用法及代码示例
- Java CompoundName getSuffix()用法及代码示例
- Java CompoundName getPrefix()用法及代码示例
- Java CompoundName startsWith()用法及代码示例
- Java CompoundName toString()用法及代码示例
- Java CompoundName equals()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 CompoundName add() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。