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

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

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

javax.naming.CompoundName 类getAll()方法用于将该复合对象的所有组件作为字符串的枚举返回。在此枚举上应用于此复合名称的更新效果未定义。

句法:

public Enumeration getAll()

参数:此方法不接受任何内容。

返回值:此方法返回此复合名称的组件的非空枚举。枚举的每个元素都属于 String 类。

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

// Java program to demonstrate
// CompoundName.getAll()
  
import java.util.Enumeration;
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 compoundName
            = new CompoundName(
                "a:b:z:y:x",
                props);
  
        // apply getAll()
        Enumeration components
            = compoundName.getAll();
  
        // print value
        int i = 0;
        while (components.hasMoreElements()) {
            System.out.println(
                "position " + i + " :"
                + components.nextElement());
            i++;
        }
    }
}
输出:
position 0 :a
position 1 :b
position 2 :z
position 3 :y
position 4 :x

方案二:

// Java program to demonstrate
// CompoundName.getAll() method
  
import java.util.Enumeration;
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 compoundName
            = new CompoundName(
                "c/e/d/v/a/b/z/y/x/f",
                props);
  
        // apply getAll()
        Enumeration components
            = compoundName.getAll();
  
        // print value
        int i = 0;
        while (components.hasMoreElements()) {
            System.out.println(
                "position " + i
                + " :" + components.nextElement());
            i++;
        }
    }
}
输出:
position 0 :c
position 1 :e
position 2 :d
position 3 :v
position 4 :a
position 5 :b
position 6 :z
position 7 :y
position 8 :x
position 9 :f

参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#getAll()