📜  Java中的 CharBuffer put() 方法

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

Java中的 CharBuffer put() 方法

放(char i)

Java.nio.CharBuffer类的put(char i)方法用于将给定的字符写入新创建的 char 缓冲区的当前位置,然后增加位置。

句法 :

public abstract CharBuffer put(char i)

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

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

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

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

以下是说明 put(char i) 方法的示例:

示例 1:

Java
// Java program to demonstrate
// put(char i) method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer using put() method
            cb.put('a');
            cb.put('b');
            cb.put('c');
            cb.rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb.array()));
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Java
// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer using put() method
            cb.put('a');
            cb.put('b');
            cb.put('c');
 
            System.out.println("Trying to put the Int at the "
                               + "position more than its limit");
            cb.put('d');
 
            // rewind the Charbuffer
            cb.rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb.array()));
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Java
// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity using allocate() method
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer cb1 = cb.asReadOnlyBuffer();
 
            System.out.println("Trying to put the Int value"
                               + " in read only buffer");
 
            // putting the value in readonly Charbuffer
            // using put() method
            cb1.put('a');
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Java
// Java program to demonstrate
// put(int index,char i) method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer using put() at index 0
            cb.put(0, 'a');
 
            // putting the value in Charbuffer using put() at index 2
            cb.put(2, 'c');
 
            // putting the value in Charbuffer using put() at index 1
            cb.put(1, 'b');
 
            // rewinding the Charbuffer
            cb.rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb.array()));
        }
 
        catch (IndexOutOfBoundsException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Java
// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer
            // using put() at index 0
            cb.put(0, 'c');
 
            // putting the value in Charbuffer
            // using put() at index 2
            cb.put(2, 'b');
 
            // putting the value in Charbuffer
            // using put() at index -1
            System.out.println("Trying to put the value"
                               + " at the negative index");
            cb.put(-1, 'a');
        }
 
        catch (IndexOutOfBoundsException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Java
// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity using allocate() method
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer cb1 = cb.asReadOnlyBuffer();
 
            System.out.println("Trying to put the Int value"
                               + " in read only buffer");
 
            // putting the value in readonly Charbuffer
            // using put() method
            cb1.put(0, 'a');
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


输出:
Original CharBuffer: [a, b, c]



示例 2:演示 BufferOverflowException。

Java

// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer using put() method
            cb.put('a');
            cb.put('b');
            cb.put('c');
 
            System.out.println("Trying to put the Int at the "
                               + "position more than its limit");
            cb.put('d');
 
            // rewind the Charbuffer
            cb.rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb.array()));
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Trying to put the Int at the position more than its limit
Exception throws : java.nio.BufferOverflowException



示例 3:演示 ReadOnlyBufferException。

Java

// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity using allocate() method
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer cb1 = cb.asReadOnlyBuffer();
 
            System.out.println("Trying to put the Int value"
                               + " in read only buffer");
 
            // putting the value in readonly Charbuffer
            // using put() method
            cb1.put('a');
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Trying to put the Int value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException



put(int index, char i)

Java.nio.CharBuffer 类的 put(int index, char i) 方法用于将给定的 int 写入到给定索引的缓冲区中。

句法:

public abstract CharBuffer put(int index, char i)

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

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

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

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

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

下面是说明 put(int index, char i) 方法的示例:

示例 1:

Java

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



示例 2:演示 IndexOutOfBoundsException。

Java

// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer
            // using put() at index 0
            cb.put(0, 'c');
 
            // putting the value in Charbuffer
            // using put() at index 2
            cb.put(2, 'b');
 
            // putting the value in Charbuffer
            // using put() at index -1
            System.out.println("Trying to put the value"
                               + " at the negative index");
            cb.put(-1, 'a');
        }
 
        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

// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity using allocate() method
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer cb1 = cb.asReadOnlyBuffer();
 
            System.out.println("Trying to put the Int value"
                               + " in read only buffer");
 
            // putting the value in readonly Charbuffer
            // using put() method
            cb1.put(0, 'a');
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}
输出:
Trying to put the Int value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException