diff --git a/content/java/concepts/queue/terms/add/add.md b/content/java/concepts/queue/terms/add/add.md new file mode 100644 index 00000000000..a1d156e39f8 --- /dev/null +++ b/content/java/concepts/queue/terms/add/add.md @@ -0,0 +1,71 @@ +--- +Title: '.add()' +Description: 'Inserts an element at the back of the queue.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Collections' + - 'Data Structures' + - 'Methods' + - 'Queues' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +The **`.add()`** method is a built-in method of the [`Queue`](https://www.codecademy.com/resources/docs/java/queue) interface in Java that inserts an element at the back of the `queue`. It returns true if the element is successfully added. Unlike `.offer()`, which returns false if the insertion fails, `.add()` throws an [`Exception`](https://www.codecademy.com/resources/docs/java/errors). This method is commonly used with implementations such as `LinkedList` and `ArrayDeque`. + +## Syntax + +```pseudo +queue_name.add(E element); +``` + +**Parameters:** + +The `.add()` method requires an `element` as a parameter. The `E` is the type of the `element` parameter, which is the type of elements held in the `queue_name` [collection](https://www.codecademy.com/resources/docs/java/collection). + +**Return value:** + +The `.add()` method returns `true` upon success. If the queue has a capacity limit and is full, `.add()` throws an `IllegalStateException`. + +## Example + +In this example, a `Queue` in Java is created using `LinkedList`, elements are added to it, and then the `.add()` method is used to insert an additional element at the end of the queue: + +```java +import java.util.*; +class JAVA { + public static void main(String args[]){ + Queue myQueue = new LinkedList(); + myQueue.add(5); + myQueue.add(2); + myQueue.add(11); + myQueue.add(7); + myQueue.add(8); + + // myQueue before adding 3: 5 => 2 => 11 => 7 => 8 + System.out.println("The elements in the queue are: " + myQueue); + + // adding 3 to myQueue + boolean success = myQueue.add(3); + System.out.println("3 inserted at the back of the queue: " + success); + + // myQueue after adding 3: 5 => 2 => 11 => 7 => 8 => 3 + System.out.println("The elements in the queue are: " + myQueue); + } +} +``` + +The output generated by this code is: + +```shell +The elements in the queue are: [5, 2, 11, 7, 8] +3 inserted at the back of the queue: true +The elements in the queue are: [5, 2, 11, 7, 8, 3] +``` + +Visual representation of the `.add()` method: + +![A diagram showing the addition of an item to a queue. The top row displays a queue with the numbers 5, 2, 11, 7, and 8. Below, the code "queue_name.add(3);" is shown, with an arrow pointing to the end of the queue. The bottom row shows the updated queue: 5, 2, 11, 7, 8, 3, illustrating that the number 3 was added to the end of the queue.](https://raw.githubusercontent.com/Codecademy/docs/main/media/java-queue-add.png) diff --git a/media/java-queue-add.png b/media/java-queue-add.png new file mode 100644 index 00000000000..8d001eac270 Binary files /dev/null and b/media/java-queue-add.png differ