java.nio.ShortBuffer类的compareTo()方法用于将一个缓冲区与另一个缓冲区进行比较。
通过按字典顺序比较两个短缓冲区的其余元素序列来比较两个短缓冲区,而不考虑每个序列在其相应缓冲区中的开始位置。通过调用Short.compare(short,short)来比较短元素对。短缓冲区不能与任何其他类型的对象进行比较。
用法:
public int compareTo(ShortBuffer that)
参数:此方法将shortbuffer对象作为参数,与该缓冲区进行比较。
返回值:此方法返回负整数,零或正整数,因为此缓冲区小于,等于或大于给定缓冲区。
下面是说明compareTo()方法的示例:
程序1:当两个FloatBuffer相等时。
// Java program to demonstrate
// compareTo() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the sb
int capacity1 = 3;
// Creating the ShortBuffer
try {
// creating object of shortbuffer sb
// and allocating size capacity
ShortBuffer sb = ShortBuffer.allocate(capacity1);
// putting the value in sb
sb.put((short)956);
sb.put((short)761);
sb.put((short)461);
// revind the short buffer
sb.rewind();
// print the ShortBuffer
System.out.println(" ShortBuffer sb: "
+ Arrays.toString(sb.array()));
// creating object of shortbuffer sb1
// and allocating size capacity
ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
// putting the value in sb1
sb1.put((short)956);
sb1.put((short)761);
sb1.put((short)461);
// revind the short buffer
sb1.rewind();
// print the shortBuffer
System.out.println(" ShortBuffer sb1: "
+ Arrays.toString(sb1.array()));
// compare both buffer and store the value into integer
int i = sb.compareTo(sb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are lexicographically equal ");
else if (i >= 0)
System.out.println("\nfb is lexicographically greater than fb1 ");
else
System.out.println("\nfb is lexicographically less than fb1 ");
}
catch (IllegalArgumentException e) {
System.out.println(" Exception throws: "
+ e);
}
catch (ReadOnlyBufferException e) {
System.out.println(" Exception throws: "
+ e);
}
}
}
输出:
ShortBuffer sb: [956, 761, 461] ShortBuffer sb1: [956, 761, 461] both buffer are lexicographically equal
程序2:当此ShortBuffer大于传递的ShortBuffer时
// Java program to demonstrate
// compareTo() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the sb
int capacity1 = 3;
// Creating the ShortBuffer
try {
// creating object of shortbuffer sb
// and allocating size capacity
ShortBuffer sb = ShortBuffer.allocate(capacity1);
// putting the value in sb
sb.put((short)956);
sb.put((short)761);
sb.put((short)41);
// revind the short buffer
sb.rewind();
// print the ShortBuffer
System.out.println(" ShortBuffer s: "
+ Arrays.toString(sb.array()));
// creating object of shortbuffer sb1
// and allocating size capacity
ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
// putting the value in sb1
sb1.put((short)856);
sb1.put((short)761);
sb1.put((short)461);
// revind the short buffer
sb1.rewind();
// print the ShortBuffer
System.out.println(" ShortBuffer sb1: "
+ Arrays.toString(sb1.array()));
// compare both buffer and store the value into integer
int i = sb.compareTo(sb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are lexicographically equal ");
else if (i >= 0)
System.out.println("\nfb is lexicographically greater than fb1 ");
else
System.out.println("\nfb is lexicographically less than fb1 ");
}
catch (IllegalArgumentException e) {
System.out.println(" Exception throws: "
+ e);
}
catch (ReadOnlyBufferException e) {
System.out.println(" Exception throws: "
+ e);
}
}
}
输出:
ShortBuffer s: [956, 761, 41] ShortBuffer sb1: [856, 761, 461] fb is lexicographically greater than fb1
程序3:当此ShortBuffer小于传递的ShortBuffer时
// Java program to demonstrate
// compareTo() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the sb
int capacity1 = 3;
// Creating the ShortBuffer
try {
// creating object of shortbuffer sb
// and allocating size capacity
ShortBuffer sb = ShortBuffer.allocate(capacity1);
// putting the value in sb
sb.put((short)856);
sb.put((short)761);
sb.put((short)461);
// revind the short buffer
sb.rewind();
// print the ShortBuffer
System.out.println(" ShortBuffer sb: "
+ Arrays.toString(sb.array()));
// creating object of shortbuffer sb1
// and allocating size capacity
ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
// putting the value in sb1
sb1.put((short)956);
sb1.put((short)761);
sb1.put((short)461);
// revind the short buffer
sb1.rewind();
// print the ShortBuffer
System.out.println(" ShortBuffer sb1: "
+ Arrays.toString(sb1.array()));
// compare both buffer and store the value into integer
int i = sb.compareTo(sb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are lexicographically equal ");
else if (i >= 0)
System.out.println("\nfb is lexicographically greater than fb1 ");
else
System.out.println("\nfb is lexicographically less than fb1 ");
}
catch (IllegalArgumentException e) {
System.out.println(" Exception throws: "
+ e);
}
catch (ReadOnlyBufferException e) {
System.out.println(" Exception throws: "
+ e);
}
}
}
输出:
ShortBuffer sb: [856, 761, 461] ShortBuffer sb1: [956, 761, 461] fb is lexicographically less than fb1
相关用法
- Java ShortBuffer asReadOnlyBuffer()用法及代码示例
- Java ShortBuffer compact()用法及代码示例
- Java ShortBuffer arrayOffset()用法及代码示例
- Java ShortBuffer equals()用法及代码示例
- Java ShortBuffer toString()用法及代码示例
- Java ShortBuffer put(int, short)用法及代码示例
- Java ShortBuffer hashCode()用法及代码示例
- Java ShortBuffer array()用法及代码示例
- Java ShortBuffer hasArray()用法及代码示例
- Java ShortBuffer duplicate()用法及代码示例
- Java ShortBuffer mark()用法及代码示例
- Java ShortBuffer slice()用法及代码示例
- Java ShortBuffer rewind()用法及代码示例
- Java ShortBuffer reset()用法及代码示例
- Java ShortBuffer allocate()用法及代码示例
注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 ShortBuffer compareTo method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。