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

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

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

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

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

    句法:

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

    参数:此方法接受:

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

    返回值:此方法返回更新的复合名称,而不是新的。返回值不能为空。

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

    下面的程序说明了 CompoundName.add() 方法:
    方案一:

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


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


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


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

    方案二:

    Java

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

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

    返回值:此方法返回更新的复合名称,而不是新的。返回值不能为空。

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

    下面的程序说明了 CompoundName.add() 方法:
    方案一:

    Java

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

    方案二:

    Java

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