Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the array() method of the IntBuffer class in Java?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In Java, the array() method of the IntBuffer class returns the array that backs a provided IntBuffer object.

The process is illustrated below:

Any changes to the IntBuffer object’s content are reflected in the array returned by the array() method.

To use the array() method, you will need to import the IntBuffer class into your program, as shown below:

import java.nio.IntBuffer

The prototype of the array() method is shown below:

public final int[] array()

Parameters

The array() method does not accept any parameters.

Return value

The array() method returns an integer array that backs the given IntBuffer object.

If the buffer is backed by an array but is set to read-only, then the array() method throws the ReadOnlyBufferException.

Similarly, if the buffer is not backed by an array, the array() method throws the UnsupportedOperationException.

Example

The code below shows how the array() method can be used in Java:

import java.nio.*;
import java.util.*;
class equalsMethod {
public static void main(String[] args) {
// initialize IntBuffer instance
IntBuffer buffer = IntBuffer.allocate(6);
int values[] = {2, 1, 6, 7, 10};
// add values to buffer
for(int i = 0; i < 5; i++)
{
buffer.put(values[i]);
}
// get array that backs buffer
int [] bufferArray = buffer.array();
// Print buffer array
System.out.println("The buffer array is: " + Arrays.toString(bufferArray));
// update buffer
buffer.put(20);
bufferArray = buffer.array();
System.out.println("The new buffer array is: " + Arrays.toString(bufferArray));
// create a read-only copy
IntBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
// get backing array
try
{
int [] readOnlyArray = readOnlyBuffer.array();
}
catch (ReadOnlyBufferException error)
{
System.out.println("An exception was thrown: " + error);
}
}
}

Explanation

First, an IntBuffer object called buffer is initialized through the allocate() method. buffer has the capacity to store 66 elements.

Next, a for-loop adds values to the buffer through the put() method.

The array() method in line 1919 proceeds to return the backing array for the buffer object. The contents of the array reflect the contents of buffer. Therefore, the addition of new values to buffer changes the backing array. As a result, the array() method in line 2626 returns a different array to the one before.

Finally, a read-only copy of buffer is created through the asReadOnlyBuffer() method. Therefore, the array() method in line 3535 throws the ReadOnlyBufferException when it tries to return the backing array.

RELATED TAGS

java

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring