Java中的 CompoundName size() 方法和示例
javax.naming.CompoundName 类的size()方法用于返回复合名称对象的大小,该大小等于该复合名称中的组件数。
句法:
public int size()
参数:此方法不接受任何内容。
返回值:此方法返回此复合名称中非负数的组件。
下面的程序说明了 CompoundName.size() 方法:
方案一:
// Java program to demonstrate
// CompoundName.size()
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(
"a:b:z:y:x", props);
// apply size()
int size = CompoundName1.size();
// print value
System.out.println("size: "
+ size);
}
}
输出:
size: 5
方案二:
// Java program to demonstrate
// CompoundName.size() 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 size()
int size = CompoundName1.size();
// print value
System.out.println("size:" + size);
}
}
输出:
size:9
参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#size()