ShortBuffer array() Method in Java with Examples
The array() method of java.nio.ShortBuffer is used to return the short array that backs this buffer(optional).
Modifications to this buffer’s content will cause the returned array’s content to be modified, and vice versa.
Invoke the hasArray method before invoking this method in order to ensure that this buffer has an accessible backing array.
public final short array()
Parameters: The method does not take any parameters.
Return Value: The method returns the array that backs the buffer.
- ReadOnlyBufferException, If the buffer is backed by an array but is read-only
- UnsupportedOperationException, If this buffer is not backed by an accessible array
Below programs illustrate the use of array() method:
ShortBuffer: [856, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Program 2: To show ReadOnlyBufferException
ShortBuffer sb: [96, 0, 7, 41, 0, 0, 0, 0, 0, 0] ShortBuffer sb1: [0, 445, 64, 0, 0] ReadOnlyBuffer ShortBuffer: 96, 0, 7, 41, 0, 0, 0, 0, 0, 0, Trying to get the array from ReadOnlysb for editing Exception thrown: java.nio.ReadOnlyBufferException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.