poner (char i)
El método put(char i) de java.nio.CharBuffer Class se usa para escribir el carácter dado en el búfer char recién creado en la posición actual y luego incrementa la posición.
Sintaxis:
public abstract CharBuffer put(char i)
Parámetros: este método toma el valor de char i como un parámetro que se escribirá en el búfer de char.
Valor devuelto: este método devuelve este búfer, en el que se inserta el valor de char.
Excepción: este método arroja las siguientes excepciones:
- BufferOverflowException: si la posición actual de este búfer no es más pequeña que su límite
- ReadOnlyBufferException : si este búfer es de solo lectura
A continuación se muestran los ejemplos para ilustrar el método put(char i):
Ejemplo 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); } } }
Original CharBuffer: [a, b, c]
Ejemplo 2: Para demostrar 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
Ejemplos 3: Para demostrar 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
poner (índice int, char i)
El método put(int index, char i) de java.nio.CharBuffer Class se usa para escribir el int dado en el búfer en el índice dado.
Sintaxis:
public abstract CharBuffer put(int index, char i)
Parámetros: Este método toma los siguientes argumentos como parámetro:
- index : el índice en el que se escribirá el carácter
- i : El valor del carácter que se va a escribir
Valor devuelto: este método devuelve este búfer.
Excepción: este método arroja la siguiente excepción:
- IndexOutOfBoundsException: si el índice es negativo o no más pequeño que el límite del búfer
- ReadOnlyBufferException : si este búfer es de solo lectura
A continuación se muestran los ejemplos para ilustrar el método put(int index, char i):
Ejemplo 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]
Ejemplo 2: Para demostrar 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
Ejemplo 3: Para demostrar 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