当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java FloatBuffer duplicate()用法及代码示例


java.nio.FloatBuffer类的duplicate()方法用于创建共享给定缓冲区内容的新浮点缓冲区。

新缓冲区的内容就是该缓冲区的内容。对该缓冲区内容的更改将在新缓冲区中可见,反之亦然;这两个缓冲区的位置,限制和标记值将是独立的。

新缓冲区的容量,限制,位置和标记值将与此缓冲区相同。当且仅当该缓冲区是直接缓冲区时,新缓冲区才是直接缓冲区;当且仅当该缓冲区是只读缓冲区时,新缓冲区才是只读缓冲区。


用法:

public abstract FloatBuffer duplicate()

返回值:此方法返回新的float缓冲区,该缓冲区带有先前的float缓冲区内容

下面是说明duplicate()方法的示例:

范例1:使用直接floatbuffer

// Java program to demonstrate 
// duplicate() method 
// Using direct floatbuffer 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the FloatBuffer 
        int capacity = 10; 
  
        // Creating the FloatBuffer 
        try { 
  
            // creating object of floatbuffer 
            // and allocating size capacity 
            FloatBuffer fb1 = FloatBuffer.allocate(capacity); 
  
            // putting the value in floatbuffer 
            fb1.put(8.56F); 
            fb1.put(2, 9.61F); 
            fb1.rewind(); 
  
            // print the Original FloatBuffer 
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb1.array())); 
  
            // Creating a duplicate copy of FloatBuffer 
            // using duplicate() method 
            FloatBuffer fb2 = fb1.duplicate(); 
  
            // print the duplicate copy of FloatBuffer 
            System.out.print("\nDuplicate FloatBuffer: "
                             + Arrays.toString(fb2.array())); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
输出:
Original FloatBuffer:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Duplicate FloatBuffer: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

范例2:使用read-onlyfloatbuffer

// Java program to demonstrate 
// duplicate() method 
// using read-onlyfloatbuffer 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the FloatBuffer 
        int capacity = 10; 
  
        // Creating the FloatBuffer 
        try { 
  
            // creating object of floatbuffer 
            // and allocating size capacity 
            FloatBuffer fb1 = FloatBuffer.allocate(capacity); 
  
            // putting the value in floatbuffer 
            fb1.put(8.56F); 
            fb1.put(2, 9.61F); 
            fb1.rewind(); 
  
            // print the Original FloatBuffer 
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb1.array())); 
  
            // Creating a read-only copy of FloatBuffer 
            // using asReadOnlyBuffer() method 
            FloatBuffer readonly = fb1.asReadOnlyBuffer(); 
  
            // print the read-only copy of FloatBuffer 
            System.out.print("\nread-only FloatBuffer:  "); 
            while (readonly.hasRemaining()) 
                System.out.print(readonly.get() + ", "); 
            System.out.println(""); 
  
            // Rewinding the readonly FloatBuffer 
            readonly.rewind(); 
  
            // Creating a duplicate copy of FloatBuffer 
            // using duplicate() method 
            FloatBuffer fb2 = readonly.duplicate(); 
  
            // print the duplicate copy of FloatBuffer 
            System.out.print("\nduplicate copy of read-only FloatBuffer:  "); 
  
            while (fb2.hasRemaining()) 
                System.out.print(fb2.get() + ", "); 
            System.out.println(""); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
输出:
Original FloatBuffer:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

read-only FloatBuffer:  8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 

duplicate copy of read-only FloatBuffer:  8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 FloatBuffer duplicate() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。