// BUILDING THIS WITH AN AI AGENT (2026)
Whether you are using Claude Code, Cursor, Windsurf, GitHub Copilot, Gemini Code Assist, Aider, or Cline — the productive pattern for production code in this space is: scaffold first, then ask for the idiomatic refactor. The prompt sequence:
Agents are good at idiomatic code when you explicitly ask — they default to older patterns otherwise.
Example: Create an ArrayList called books which stores book names
import java.util.List;
import java.util.ArrayList;
public Class ArrayListDemo {
List<String> books = new ArrayList<String>();
} ArrayList and List are under Java utils package. List interface has other implementations besides ArrayList the below image shows them all.
The method is used to add elements into the ArrayList, let’s add a couple of books to the ArrayList we created above
books.add("The Alchemist");
books.add("Four Hour Work Week");
books.add("Little Fires Everywhere"); The method is used to clear all elements from the ArrayList.
books.clear(); // clears the Arraylist books The method remove takes index as a parameter and removes elements from that index.
books.remove(1); //removes the book titled "Four Hour Work Week" from the arraylist. The method checks if the element exists in the ArrayList and returns true or false.
books.contains("Steve Jobs"); // returns boolean value false as it doesn't exists. The method is used to retrieve the ArrayList element from a particular index.
books.get(0); // returns "The Alchemist" as that is the book at index 0. This method is used to find the index of an element in the ArrayList.
books.index("Little Fires Everywhere"); //returns 2 the index of the book in the arraylist. Checks if the ArrayList is empty and returns a boolean value.
books.isEmpty();// returns false if not empty. This method sorts the ArrayList in alphabetical order if it’s of type String or numerical if Integer type or follows the overridden methods if comparable methods are overridden.
books.sort();//reuslts is {"Four Hour Work Week", "Little Fires Everywhere", "The Alchemist"} This method returns the size of the ArrayList.
books.size(); // return 3 the size of ArrayList. This method is used to replace an element at a particular index.
books.set(1, "Start With Why"); //results in {"Four Hour Work Week", "Start With Why", "The Alchemist"} These are some of the most commonly used methods in an ArrayList in Java.
Below is the video tutorial explaining with a live demonstration.
Please subscribe to our Youtube Channel HackerHeap ClickHere
For the AI-native engineering side of HackerHeap — building MCP servers, comparing agents (Claude Code, Cursor, Windsurf, Codex, Gemini, Copilot), and weekly working code — see the Friday Build newsletter and the MCP archive.
HackerHeap is back: a multi-platform resource for working developers building with AI coding agents. We…
// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…
// BUILDING THIS WITH AN AI AGENT (2026)Whether you are using Claude Code, Cursor, Windsurf,…
// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…
// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…
// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…
This website uses cookies.