Java IntBuffer compareTo()方法

Java IntBuffer compareTo()方法

java.nio.IntBuffer 类的 compareTo() 方法是用来比较一个缓冲区和另一个缓冲区。两个int缓冲区通过比较它们的剩余元素序列进行比较,不考虑每个序列在其相应缓冲区中的起始位置。一对int元素的比较是通过调用Int.compare(int, int)进行的,除了-0和0被认为是相等的。本方法认为Int.NaN等于自己,并且大于所有其他的int值(包括Int.POSITIVE_INFINITY)。一个int缓冲区不能与任何其他类型的对象相提并论。

语法:

public int compareTo(IntBuffer that)

参数 :该方法接受一个 intbuffer对象 作为参数,该缓冲区将与之进行比较。
返回值: 该方法在该缓冲区小于、等于或大于给定的缓冲区时返回一个 负整数、零或正整数 。下面的程序说明了 compareTo() 方法:

例子1: 当两个IntBuffer相等时。

// 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 ib
        int capacity1 = 3;
 
        // Creating the IntBuffer
        try {
 
            // creating object of Intbuffer ib
            // and allocating size capacity
            IntBuffer ib = IntBuffer.allocate(capacity1);
 
            // putting the value in ib
            ib.put(9);
            ib.put(7);
            ib.put(4);
 
            // rewind the Int buffer
            ib.rewind();
 
            // print the IntBuffer
            System.out.println("IntBuffer ib: "
                               + Arrays.toString(ib.array()));
 
            // creating object of Intbuffer ib1
            // and allocating size capacity
            IntBuffer ib1 = IntBuffer.allocate(capacity1);
 
            // putting the value in ib1
            ib1.put(9);
            ib1.put(7);
            ib1.put(4);
 
            // rewind the Int buffer
            ib1.rewind();
 
            // print the IntBuffer
            System.out.println("IntBuffer ib1: "
                               + Arrays.toString(ib1.array()));
 
            // compare both buffer and store the value into integer
            int i = ib.compareTo(ib1);
 
            // if else condition
            if (i == 0)
                System.out.println("\nBoth buffer are lexicographically equal");
            else if (i >= 0)
                System.out.println("\nib is lexicographically greater than ib1");
            else
                System.out.println("\nib is lexicographically less than ib1");
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

IntBuffer ib: [9, 7, 4]
IntBuffer ib1: [9, 7, 4]

Both buffer are lexicographically equal

例2: 当这个IntBuffer大于传递的IntBuffer时

// 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 ib
        int capacity1 = 3;
 
        // Creating the IntBuffer
        try {
 
            // creating object of Intbuffer ib
            // and allocating size capacity
            IntBuffer ib = IntBuffer.allocate(capacity1);
 
            // putting the value in ib
            ib.put(9);
            ib.put(7);
            ib.put(4);
 
            // rewind the Int buffer
            ib.rewind();
 
            // print the IntBuffer
            System.out.println("IntBuffer ib: "
                               + Arrays.toString(ib.array()));
 
            // creating object of Intbuffer ib1
            // and allocating size capacity
            IntBuffer ib1 = IntBuffer.allocate(capacity1);
 
            // putting the value in ib1
            ib1.put(8);
            ib1.put(7);
            ib1.put(4);
 
            // rewind the Int buffer
            ib1.rewind();
 
            // print the IntBuffer
            System.out.println("IntBuffer ib1: "
                               + Arrays.toString(ib1.array()));
 
            // compare both buffer and store the value into integer
            int i = ib.compareTo(ib1);
 
            // if else condition
            if (i == 0)
                System.out.println("\nBoth buffer are lexicographically equal");
            else if (i >= 0)
                System.out.println("\nib is lexicographically greater than ib1");
            else
                System.out.println("\nib is lexicographically less than ib1");
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

IntBuffer ib: [9, 7, 4]
IntBuffer ib1: [8, 7, 4]

ib is lexicographically greater than ib1

例3: 当这个IntBuffer小于传递的IntBuffer时。

// 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 ib
        int capacity1 = 3;
 
        // Creating the IntBuffer
        try {
 
            // creating object of Intbuffer ib
            // and allocating size capacity
            IntBuffer ib = IntBuffer.allocate(capacity1);
 
            // putting the value in ib
            ib.put(8);
            ib.put(7);
            ib.put(4);
 
            // rewind the Int buffer
            ib.rewind();
 
            // print the IntBuffer
            System.out.println("IntBuffer ib: "
                               + Arrays.toString(ib.array()));
 
            // creating object of Intbuffer ib1
            // and allocating size capacity
            IntBuffer ib1 = IntBuffer.allocate(capacity1);
 
            // putting the value in ib1
            ib1.put(9);
            ib1.put(7);
            ib1.put(4);
 
            // rewind the Int buffer
            ib1.rewind();
 
            // print the IntBuffer
            System.out.println("IntBuffer ib1: "
                               + Arrays.toString(ib1.array()));
 
            // compare both buffer and store the value into integer
            int i = ib.compareTo(ib1);
 
            // if else condition
            if (i == 0)
                System.out.println("\nBoth buffer are lexicographically equal");
            else if (i >= 0)
                System.out.println("\nib is lexicographically greater than ib1");
            else
                System.out.println("\nib is lexicographically less than ib1");
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

输出

IntBuffer ib: [8, 7, 4]
IntBuffer ib1: [9, 7, 4]

ib is lexicographically less than ib1

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程