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

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

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

javax.naming.CompositeName 类get()方法用于获取此复合名称对象的组件。作为参数传递的位置用于从复合名称对象获取该位置存在的组件。

句法:

public String get(int posn)

参数:此方法接受posn ,它是要检索的组件的从 0 开始的索引。必须在 [0, size()) 范围内。

返回值:此方法返回索引 posn 处的组件。

异常:如果 posn 超出指定范围,此方法将引发ArrayIndexOutOfBoundsException

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

// Java program to demonstrate
// CompositeName.get()
  
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/baz/ay/x");
  
        // apply get()
        String comp1 = CompositeName1.get(0);
        String comp2 = CompositeName1.get(3);
  
        // print value
        System.out.println(comp1);
        System.out.println(comp2);
    }
}
输出:
a
x

方案二:

// Java program to demonstrate
// CompositeName.get() 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 get()
        String pos3 = CompositeName1.get(3);
        String pos4 = CompositeName1.get(4);
  
        // print value
        System.out.println(
            "Component at position 3: "
            + pos3);
        System.out.println(
            "Component at position 4: "
            + pos4);
    }
}
输出:
Component at position 3: v
Component at position 4: a

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