Java中的 CompoundName addAll() 方法及示例
javax.naming.CompoundName 类的addAll()方法用于将不同复合名称对象的所有组件添加到此 CompoundName 对象。
有两种不同的 addAll() 方法。
1. addAll(int, Name) :该方法用于在该复合名称对象的指定位置posn处添加作为参数传递的不同复合名称对象的所有组件。在第一个新组件的位置处或之后出现的该复合名称对象的其他组件向上移动以容纳不同组件的所有组件。
句法:
public Name addAll(int posn, Name n)
throws InvalidNameException
参数:此方法接受:
- posn是添加所有新组件的索引,并且
- n这是要添加的非空组件。
返回值:此方法返回更新的 CompoundName 对象,而不是新对象。返回值不能为空。
异常:此方法抛出ArrayIndexOutOfBoundsException如果 posn 超出指定范围并且 InvalidNameException 如果 n 不是复合名称,或者如果添加的组件违反了该复合名称的语法(例如,超过了组件的数量)。
下面的程序说明了 CompoundName.addAll() 方法:
方案一:
Java
// 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);
}
}
Java
// 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: 9@99@999@9999@1@2@3@4@5@6@7
2. addAll(String) :此方法用于将作为输入传递的不同复合名称对象的所有组件添加到该复合名称的末尾。
句法:
public Name addAll(Name suffix)
throws InvalidNameException
参数:此方法接受后缀,即要添加的非空组件。
返回值:此方法返回更新的复合名称,而不是新的。返回值不能为空。
异常:如果后缀不是复合名称,或者添加的组件违反了该复合名称的语法(例如,超过了组件的数量),则此方法抛出InvalidNameException 。
下面的程序说明了 CompoundName.addAll() 方法:
方案一:
Java
// 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)