📌  相关文章
📜  Java中的 CompositeName add() 方法及示例

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

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

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

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

句法:

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

参数:此方法接受:

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

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

异常:此方法抛出ArrayIndexOutOfBoundsException If posn is outside the specified range and InvalidNameException If 在指定位置添加 comp 将违反复合名称的语法。

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

方案一:

Java
// 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);
    }
}


Java
// 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);
    }
}


Java
// 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);
    }
}


Java
// 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: 000/1/2/3/4/5/6/7

方案二:

Java

// 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. add(String) :该方法用于将单个组件添加到此复合名称的末尾。

句法:

public Name add(String comp)
       throws InvalidNameException

参数:此方法接受comp ,它是要在最后添加到复合名称对象中的新组件。

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

异常:此方法抛出InvalidNameException如果在名称末尾添加 comp 将违反复合名称的语法。

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

方案一:

Java

// 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

方案二:

Java

// 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 Java)