javax.naming.CompoundName類的compareTo()方法用於比較此CompoundName與作為參數傳遞的指定對象。當此CompoundName對象小於,等於或大於給定Object作為參數時,它將返回負整數,零或正整數。如果傳遞的對象為null或不是CompoundName的實例,則此方法將引發ClassCastException。
用法:
public int compareTo(Object obj)
參數:此方法接受obj,它是要與之比較的非null對象。
返回值:此方法返回負整數,零或正整數,因為此Name小於,等於或大於給定的Object。
異常:如果傳遞的obj不是CompoundName對象,則此方法將引發ClassCastException。
以下示例程序旨在說明CompoundName.compareTo()方法:
程序1:
// Java program to demonstrate
// CompoundName.compareTo()
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("x:y:z",
props);
CompoundName CompoundName2
= new CompoundName("x:y:m",
props);
// apply compareTo()
int value
= CompoundName1
.compareTo(CompoundName2);
// print value
if (value > 0)
System.out.println(
"CompoundName1 is "
+ "greater than CompoundName2");
else if (value < 0)
System.out.println(
"CompoundName1 is "
+ "smaller than CompoundName2");
else
System.out.println(
"CompoundName1 is "
+ "equal to CompoundName2");
}
}
輸出:
CompoundName1 is greater than CompoundName2
程序2:
// Java program to demonstrate
// CompoundName.compareTo() 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(
"x@y@z@M@n",
props);
CompoundName CompoundName2
= new CompoundName(
"x@y@z@M@n",
props);
// apply compareTo()
int value
= CompoundName1
.compareTo(CompoundName2);
// print value
if (value > 0)
System.out.println(
"CompoundName1 is "
+ "greater than CompoundName2");
else if (value < 0)
System.out.println(
"CompoundName1 is "
+ "smaller than CompoundName2");
else
System.out.println(
"CompoundName1 is "
+ "equal to CompoundName2");
}
}
輸出:
CompoundName1 is equal to CompoundName2
相關用法
- Java CompoundName get()用法及代碼示例
- Java CompoundName add()用法及代碼示例
- Java CompoundName getPrefix()用法及代碼示例
- Java CompoundName equals()用法及代碼示例
- Java CompoundName endsWith()用法及代碼示例
- Java CompoundName hashCode()用法及代碼示例
- Java CompoundName size()用法及代碼示例
- Java CompoundName startsWith()用法及代碼示例
- Java CompoundName getAll()用法及代碼示例
- Java CompoundName clone()用法及代碼示例
- Java CompoundName isEmpty()用法及代碼示例
- Java CompoundName remove()用法及代碼示例
- Java CompoundName toString()用法及代碼示例
- Java CompoundName addAll()用法及代碼示例
- Java CompoundName getSuffix()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 CompoundName compareTo() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。