📜  Java中的 CharBuffer order() 方法及示例

📅  最后修改于: 2022-05-13 01:55:05.100000             🧑  作者: Mango

Java中的 CharBuffer order() 方法及示例

Java.nio.CharBuffer类的order()方法用于检索此缓冲区的字节顺序。通过分配或包装现有 char 数组创建的 char 缓冲区的字节顺序是底层硬件的本机顺序。作为字节缓冲区的视图创建的字符缓冲区的字节顺序是创建视图时字节缓冲区的字节顺序。
句法:

public abstract ByteOrder order()

返回值:此方法返回此缓冲区的字节顺序。
以下是说明 order() 方法的示例:
示例 1:

Java
// Java program to demonstrate
// order() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // creating object of CharBuffer
        // and allocating size capacity
        CharBuffer cb
            = CharBuffer.allocate(4);
 
        // append the int value in the charbuffer
        cb.append('a')
            .append('b')
            .append('c')
            .append('d');
 
        // rewind the Bytebuffer
        cb.rewind();
 
        // Retrieve the ByteOrder
        // using order() method
        ByteOrder order = cb.order();
 
        // print the char buffer and order
        System.out.println("CharBuffer is : "
                           + Arrays.toString(cb.array())
                           + "\nOrder: " + order);
    }
}


Java
// Java program to demonstrate
// order() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // creating object of CharBuffer
        // and allocating size capacity
        CharBuffer cb = CharBuffer.allocate(4);
 
        // Retrieve the ByteOrder
        // using order() method
        ByteOrder order = cb.order();
 
        // print the char buffer and order
        System.out.println("CharBuffer is : "
                           + Arrays.toString(cb.array())
                           + "\nOrder: " + order);
    }
}


输出:
CharBuffer is : [a, b, c, d]
Order: LITTLE_ENDIAN

示例 2:

Java

// Java program to demonstrate
// order() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // creating object of CharBuffer
        // and allocating size capacity
        CharBuffer cb = CharBuffer.allocate(4);
 
        // Retrieve the ByteOrder
        // using order() method
        ByteOrder order = cb.order();
 
        // print the char buffer and order
        System.out.println("CharBuffer is : "
                           + Arrays.toString(cb.array())
                           + "\nOrder: " + order);
    }
}
输出:
CharBuffer is : [,,,  ]
Order: LITTLE_ENDIAN

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/CharBuffer.html#order–