poner (doble f)
El método put(doble f) de java.nio.DoubleBuffer Class se usa para escribir el doble dado en el búfer doble recién creado en la posición actual y luego incrementa la posición.
Sintaxis:
public abstract DoubleBuffer put(double f)
Parámetros: este método toma el valor doble f como parámetro que se escribirá en el búfer doble.
Valor devuelto: este método devuelve este búfer , en el que se inserta el valor doble.
Excepción: este método arroja las siguientes excepciones:
- BufferOverflowException : si la posición actual de este búfer no es menor 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(doble f):
Ejemplo 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 DoubleBuffer int capacity = 3; // Creating the DoubleBuffer try { // creating object of Doublebuffer // and allocating size capacity DoubleBuffer db = DoubleBuffer.allocate(capacity); // putting the value in Doublebuffer using put() method db.put(8.56D); db.put(9.61D); db.put(7.86D); db.rewind(); // print the DoubleBuffer System.out.println("Original DoubleBuffer: " + Arrays.toString(db.array())); } catch (BufferOverflowException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
Original DoubleBuffer: [8.56, 9.61, 7.86]
Ejemplo 2: Para demostrar BufferOverflowException.
// 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 DoubleBuffer int capacity = 3; // Creating the DoubleBuffer try { // creating object of Doublebuffer // and allocating size capacity DoubleBuffer db = DoubleBuffer.allocate(capacity); // putting the value in Doublebuffer using put() method db.put(8.56F); db.put(9.61F); db.put(7.86F); System.out.println("Trying to put the Double at the " + "position more than its limit"); db.put(7.86F); // rewind the Doublebuffer db.rewind(); // print the DoubleBuffer System.out.println("Original DoubleBuffer: " + Arrays.toString(db.array())); } catch (BufferOverflowException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
Trying to put the Double at the position more than its limit Exception throws : java.nio.BufferOverflowException
Ejemplos 3: Para demostrar 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 theDoubleBuffer int capacity = 3; // Creating theDoubleBuffer try { // creating object ofDoublebuffer // and allocating size capacity using allocate() method DoubleBuffer db = DoubleBuffer.allocate(capacity); // Creating a read-only copy ofDoubleBuffer // using asReadOnlyBuffer() method DoubleBuffer db1 = db.asReadOnlyBuffer(); System.out.println("Trying to put theDouble value" + " in read only buffer"); // putting the value in readonlyDoublebuffer // using put() method db1.put(8.56F); } catch (BufferOverflowException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
Trying to put theDouble value in read only buffer Exception throws : java.nio.ReadOnlyBufferException
put(índice int, doble f)
El método put(int index, double f) de java.nio.DoubleBuffer Class se usa para escribir el doble dado en el búfer en el índice dado.
Sintaxis:
public abstract DoubleBuffer put(int index, double f)
Parámetros: Este método toma los siguientes argumentos como parámetro:
- index : el índice en el que se escribirá el doble
- f : El valor doble a escribir
Valor de retorno: 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 menor 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, double f):
Ejemplo 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 DoubleBuffer int capacity = 3; // Creating the DoubleBuffer try { // creating object of Doublebuffer // and allocating size capacity DoubleBuffer db = DoubleBuffer.allocate(capacity); // putting the value in Doublebuffer using put() at index 0 db.put(0, 8.56F); // putting the value in Doublebuffer using put() at index 2 db.put(2, 9.61F); // putting the value in Doublebuffer using put() at index 1 db.put(1, 7.86F); // rewinding the Doublebuffer db.rewind(); // print the DoubleBuffer System.out.println("Original DoubleBuffer: " + Arrays.toString(db.array())); } catch (IndexOutOfBoundsException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
Original DoubleBuffer: [8.5600004196167, 7.860000133514404, 9.609999656677246]
Ejemplo 2: Para demostrar 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 DoubleBuffer int capacity = 3; // Creating the DoubleBuffer try { // creating object of Doublebuffer // and allocating size capacity DoubleBuffer db = DoubleBuffer.allocate(capacity); // putting the value in Doublebuffer // using put() at index 0 db.put(0, 8.56F); // putting the value in Doublebuffer // using put() at index 2 db.put(2, 9.61F); // putting the value in Doublebuffer // using put() at index -1 System.out.println("Trying to put the value" + " at the negative index"); db.put(-1, 7.86F); } 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 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 DoubleBuffer int capacity = 3; // Creating the DoubleBuffer try { // creating object of Doublebuffer // and allocating size capacity using allocate() method DoubleBuffer db = DoubleBuffer.allocate(capacity); // Creating a read-only copy of DoubleBuffer // using asReadOnlyBuffer() method DoubleBuffer db1 = db.asReadOnlyBuffer(); System.out.println("Trying to put the Double value" + " in read only buffer"); // putting the value in readonly Doublebuffer // using put() method db1.put(0, 8.56F); } catch (BufferOverflowException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } } }
Trying to put the Double value in read only buffer Exception throws : java.nio.ReadOnlyBufferException
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA