當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java CompositeName get()用法及代碼示例


javax.naming.CompositeName類的get()方法用於獲取此複合名稱對象的組件。位置作為參數傳遞,用於從複合名稱對象獲取該位置上存在的組件。

用法:

public String get(int posn)

參數:此方法接受posn,它是要檢索的組件的從0開始的索引。必須在[0,size())範圍內。



返回值:此方法返回索引posn處的組件。

異常:如果posn在指定範圍之外,則此方法引發ArrayIndexOutOfBoundsException。

下麵的程序說明CompositeName.get()方法:
程序1:

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

程序2:

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




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 CompositeName get() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。