當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java DoubleBuffer put()方法用法及代碼示例


put(double f)

java.nio.DoubleBuffer類的put(double f)方法用於將給定的double寫入當前位置的新創建的double緩衝區中,然後遞增該位置。

用法:

public abstract DoubleBuffer put(double f)

參數:該方法將雙精度值f作為要寫入雙精度緩衝區的參數。



返回值:此方法返回此緩衝區,在該緩衝區中插入了double值。

異常:此方法引發以下異常:

  • BufferOverflowException-如果此緩衝區的當前位置不小於其限製
  • ReadOnlyBufferException-如果此緩衝區是隻讀的

以下示例說明了put(double f)方法:

範例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]

範例2:演示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

範例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 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(int index, double f)

java.nio.DoubleBuffer類的put(int index,double f)方法用於將給定double寫入給定索引處的緩衝區。

用法:



public abstract DoubleBuffer put(int index, double f)

參數:此方法將以下參數作為參數:

  • index:將寫入雙精度的索引
  • f:要寫入的雙精度值

返回值:此方法返回此緩衝區。

異常:此方法引發以下異常:

  • IndexOutOfBoundsException-如果索引為負或不小於緩衝區的限製
  • ReadOnlyBufferException-如果此緩衝區是隻讀的

以下示例說明了put(int index,double f)方法:

範例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]

範例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 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

範例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 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



相關用法


注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 DoubleBuffer put() methods in Java with Examples | Set 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。