📜  Java中的 CompositeName addAll() 方法及示例

📅  最后修改于: 2022-05-13 01:55:49.878000             🧑  作者: Mango

Java中的 CompositeName addAll() 方法及示例

javax.naming.CompositeName 类addAll()方法用于将不同复合名称对象的所有组件添加到此 CompositeName 对象。
有两种不同的 addAll() 方法。

1. addAll(int, Name) :该方法用于在该复合名称对象中的指定位置posn处添加作为参数传递的不同复合名称对象的所有组件。在第一个新组件的位置处或之后出现的该复合名称对象的其他组件向上移动以容纳不同组件的所有组件。

句法:

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

参数:此方法接受

  • posn是添加所有新组件的索引,并且
  • n这是要添加的非空组件。

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

异常:此方法抛出ArrayIndexOutOfBoundsException如果 posn 超出指定范围并且 InvalidNameException 如果 n 不是复合名称,或者如果添加的组件违反了该复合名称的语法(例如,超过了组件的数量)。

下面的程序说明了 CompositeName.addAll() 方法:

方案一:

Java
// Java program to demonstrate
// CompositeName.addAll()
 
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/8/9");
 
        // different component to add at position 0
        CompositeName differentComponent
            = new CompositeName("9/99/999/9999");
 
        // apply addAll() to add All new components at posn 0
        CompositeName newCompositeName
            = (CompositeName)
                  CompositeName1
                      .addAll(
                          0, differentComponent);
 
        // print value
        System.out.println(
            "Updated CompositeName Object: "
            + newCompositeName);
    }
}


Java
// Java program to demonstrate
// CompositeName.addAll()
 
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");
 
        // different component to add at position 0
        CompositeName differentComponent
            = new CompositeName("9/99/999/9999");
 
        // apply addAll() to add All
        // new components at posn 0
        CompositeName newCompositeName
            = (CompositeName)
                  CompositeName1
                      .addAll(differentComponent);
 
        // print value
        System.out.println(
            "Updated CompositeName Object: "
            + newCompositeName);
    }
}


输出:
Updated CompositeName Object: 9/99/999/9999/1/2/3/4/5/6/7/8/9

2. addAll(Name) :该方法用于将作为输入传递的不同复合名称对象的所有组件添加到该复合名称的末尾。

句法:

public Name addAll(Name suffix)
            throws InvalidNameException

参数:此方法接受后缀,即要添加的非空组件。

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

异常:如果后缀不是复合名称,或者添加的组件违反了此复合名称的语法(例如,超过了组件的数量),则此方法将抛出InvalidNameException

下面的程序说明了 CompositeName.addAll() 方法:

方案一:

Java

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

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