FloatBuffer put() methods in Java with ExamplesThe FloatBuffer put() has mainly 2 methods that take two different parameters.
i. put(float f)The java.nio.FloatBuffer class has put(float f) function. The newly generated float buffer is written with the specified float at the current location, and the position is then incremented using the FloatBuffer Class. Syntax: Parameters: The float value f, which needs to be entered into a float buffer, is the parameter for this procedure. Return Value: The float value is inserted into the buffer that this procedure returns. Exception: The following exceptions are thrown by this method: BufferOverflowException- if the position of this buffer is not now less than its limit. ReadOnlyBufferException- if this buffer can be read-on. Example 1:The code shows how to create and work with a FloatBuffer in Java. Three float values can be stored in the FloatBuffer that is allocated, due to its capacity of three. It adds three float values to the buffer by using the put function. In order to prepare the buffer for reading, the rewind method is invoked to reset its position to the beginning. The backing array of the buffer is then converted to a string representation, and the contents of the FloatBuffer are displayed using Arrays.toString. To manage possible failures during buffer operations, the code incorporates exception handling for BufferOverflowException and ReadOnlyBufferException. Implementation:FileName: putFloatExample1.java Output: The Original FloatBuffer is given by : [9.46, 8.41, 2.76] ii. put(int index, float f)The java.nio.FloatBuffer class has put(int index, float f) function. The given float is written into the buffer at the specified index using the FloatBuffer Class. Syntax: Parameters: The following arguments are passed as parameters to this method: index: The index where the float will continue to be written f: The value of the float to be written Return Value: The buffer is returned by this method. Exception: The exception that this method throws is as follows: IndexOutOfBoundsException- If the index is less than the buffer's limit, whether it is negative. ReadOnlyBufferException- If this buffer can only be read. Example 1:The code explains a Java program that shows how to store floating-point values at particular indices using a FloatBuffer. The put() function is used in the main method to insert float values at the provided indices, which are 9.35F at index 0, 8.27F at index 2, and 7.21F at index 1. The put() method also initializes a FloatBuffer with a capacity of 3. The buffer's position is reset to zero by calling the rewind() method after the values have been inserted. Lastly, an array representing the contents of the FloatBuffer is printed. To handle possible failures during buffer operations, the program has exception handling for IndexOutOfBoundsException and ReadOnlyBufferException. Implementation:FileName: putIndexExample1.java Output: The Original FloatBuffer is given by: [9.35, 7.21, 8.27] Example 2:The code illustrates a Java program that shows how to store floating-point numbers at specified indices using a FloatBuffer and how to handle errors when incorrect indices are used. The put() method is used in the main method to insert two float values, 9.25F at index 0 and 8.37F at index 2, after initializing a FloatBuffer with a capacity of three. An IndexOutOfBoundsException is then raised when it tries to insert the value 7.34F at an illegal negative index (-1). An appropriate message is printed after this exception is detected. Implementation:FileName: putIndexFloatExample2.java Output: Trying to put the value when the index is negative The Exception thrown is given by : java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3 Next TopicHorizontal-flip-matrix-problem-in-java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India