Skip to content

datastructure.array: generic DynamicArray<T> - #18

Open
diegourban wants to merge 3 commits into
masterfrom
int-dynamic-array
Open

diegourban wants to merge 3 commits into
masterfrom
int-dynamic-array

Conversation

@diegourban

@diegourban diegourban commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

A growable, generic list backed by an array: DynamicArray<T> in datastructure.array. It started as an int-only stub (IntDynamicArray); the implementation is hand-written.

API

append, get, set, size, popBack, and pop(index) (returns the removed value). Invalid indexes throw IndexOutOfBoundsException.

Growth policy

  • The capacity starts at 10.
  • It doubles when an append hits a full array.
  • It halves when a removal leaves it less than a quarter full, never dropping below 10.
  • This keeps every operation amortised O(1), apart from the shifts in pop(index).

The backing store is (T[]) new Object[n], because Java can't create an array of a type parameter. The cast is safe because only T values are ever stored. popBack clears the removed slot so the object can be garbage-collected.

Tests

  • DynamicArrayTest: 12 examples, including a String/null case and popping from an exactly full array.
  • DynamicArrayPropertyTest: jqwik model-based check. Random sequences of append, set, popBack and pop (up to 200 operations) must leave the array identical to a java.util.ArrayList after every step. In CI it found a popBack bug on a full array, now fixed.

Later

Replace the old datastructure.vector.Vector with DynamicArray<T>.

🤖 Generated with Claude Code

The class is documented (growth policy, contract, exceptions) but every
method throws UnsupportedOperationException, to be implemented by hand.

- IntDynamicArrayTest: 11 examples, incl. doubling when full and halving
  at a quarter full, never below the initial capacity of 10
- IntDynamicArrayPropertyTest: jqwik model-based check that random
  add/insert/set/remove sequences keep it identical to an ArrayList

The 12 new tests fail until the implementation lands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codacy-production

codacy-production Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 53 complexity · 2 duplication

Metric Results
Complexity 53
Duplication 2

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@diegourban diegourban changed the title datastructure.array: IntDynamicArray datastructure.array: DynamicArray Nov 18, 2026
popBack nulled fixedArray[size] before size -= 1, so on a full array it
wrote one past the end (ArrayIndexOutOfBoundsException: Index 10 out of
bounds for length 10, found by DynamicArrayPropertyTest in CI), and on
other arrays it cleared the empty slot after the last value instead of
releasing the removed one. Decrement first, then clear.

New example test popBackFromAFullArray failed before the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@diegourban diegourban changed the title datastructure.array: DynamicArray datastructure.array: generic DynamicArray<T> Nov 18, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.83%. Comparing base (73540ba) to head (bd75642).

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #18      +/-   ##
============================================
+ Coverage     99.65%   99.83%   +0.18%     
- Complexity      294      314      +20     
============================================
  Files            24       25       +1     
  Lines           574      611      +37     
  Branches        101      103       +2     
============================================
+ Hits            572      610      +38     
+ Partials          2        1       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@diegourban
diegourban marked this pull request as ready for review November 18, 2026 21:05

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

👀 Worth a closer look

The growth logic can overflow at very large capacities, causing an append to fail with an invalid array size even when growth may still be possible. This warrants review before relying on the implementation at extreme sizes.

One recommended shrinking-boundary test remains missing; no uncovered complex files were reported.

Test suggestions

  • Appending values preserves order and supports growth beyond the initial capacity.
  • Appending to an exactly full array and then removing works correctly.
  • Getting and setting valid indexes works for generic values, including null.
  • Invalid positive and negative indexes are rejected for get, set, and pop.
  • Popping from an empty array is rejected.
  • popBack removes and returns no value while pop(index) returns the removed value and shifts later elements.
  • Removing enough elements triggers shrinking while preserving remaining values.
  • Repeated shrinking never reduces capacity below the initial capacity.
  • Arbitrary operation sequences match ArrayList behavior.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Repeated shrinking never reduces capacity below the initial capacity.

TIP Add review instructions in .codacy/instructions/review.md, or an AGENTS.md, so the reviewer knows your conventions.
TIP How was this review? Give us feedback

*/
public void append(final T value) {
if (this.size == this.capacity) {
this.resize(this.capacity * 2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Capacity growth is not overflow-safe: doubling a sufficiently large capacity can wrap to a negative value and cause NegativeArraySizeException on the next append. Guard the calculation, cap it at the maximum supported array size, and report exhaustion when no further growth is possible.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants