📜  Java中的 LongBuffer put() 方法

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

Java中的 LongBuffer put() 方法

看跌(长我)

Java.nio.LongBuffer类的put(Long i)方法用于将给定的 Long 值写入到当前位置新创建的 Long 缓冲区中,然后递增该位置。

句法:

public abstract LongBuffer put(Long i)

参数:此方法将 Long 值i作为要写入 Long 缓冲区的参数。

返回值:此方法返回此缓冲区,其中插入了 Long 值。

异常:此方法抛出以下异常:

  • BufferOverflowException-如果此缓冲区的当前位置不小于其限制
  • ReadOnlyBufferException-如果此缓冲区是只读的

下面是说明 put(Long i) 方法的示例:

示例 1:

// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer using put() method
            ib.put(8);
            ib.put(9);
            ib.put(7);
            ib.rewind();
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Original LongBuffer:  [8, 9, 7]

示例 2:演示 BufferOverflowException。

// Java program to demonstrate
// put() method
  
// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer using put() method
            ib.put(8);
            ib.put(9);
            ib.put(7);
  
            System.out.println("Trying to put the Long at the "
                               + "position more than its limit");
            ib.put(7);
  
            // rewind the Longbuffer
            ib.rewind();
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Trying to put the Long at the position more than its limit
Exception throws : java.nio.BufferOverflowException

示例 3:演示 ReadOnlyBufferException。

// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity using allocate() method
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // Creating a read-only copy of LongBuffer
            // using asReadOnlyBuffer() method
            LongBuffer ib1 = ib.asReadOnlyBuffer();
  
            System.out.println("Trying to put the Long value"
                               + " in read only buffer");
  
            // putting the value in readonly Longbuffer
            // using put() method
            ib1.put(8);
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Trying to put the Long value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException

put(Long index, Long i)

Java.nio.LongBuffer类的 put(Long index, Long i) 方法用于将给定的 Long 值写入给定索引处的缓冲区。

句法:

public abstract LongBuffer put(Long index, Long i)

参数:此方法采用以下参数作为参数:

  • index : Long 将被写入的索引
  • i : 要写入的 Long 值

返回值:此方法返回此缓冲区。

异常:此方法抛出以下异常:

  • IndexOutOfBoundsException-如果 index 为负数或不小于缓冲区的限制
  • ReadOnlyBufferException-如果此缓冲区是只读的

以下是说明 put(Long index, Long i) 方法的示例:

示例 1:

// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer using put() at  index 0
            ib.put(0, 8);
  
            // putting the value in Longbuffer using put() at  index 2
            ib.put(2, 9);
  
            // putting the value in Longbuffer using put() at  index 1
            ib.put(1, 7);
  
            // rewinding the Longbuffer
            ib.rewind();
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Original LongBuffer:  [8, 7, 9]

示例 2:演示 IndexOutOfBoundsException。

// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer
            // using put() at  index 0
            ib.put(0, 8);
  
            // putting the value in Longbuffer
            // using put() at  index 2
            ib.put(2, 9);
  
            // putting the value in Longbuffer
            // using put() at  index -1
            System.out.println("Trying to put the value"
                               + " at the negative index");
            ib.put(-1, 7);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Trying to put the value at the negative index
Exception throws : java.lang.IndexOutOfBoundsException

示例 3:演示 ReadOnlyBufferException。

// Java program to demonstrate
// put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity using allocate() method
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // Creating a read-only copy of LongBuffer
            // using asReadOnlyBuffer() method
            LongBuffer ib1 = ib.asReadOnlyBuffer();
  
            System.out.println("Trying to put the Long value"
                               + " in read only buffer");
  
            // putting the value in readonly Longbuffer
            // using put() method
            ib1.put(0, 8);
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Trying to put the Long value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException