Created
September 30, 2025 19:51
-
-
Save MrKomodoDragon/8657c4c7137f29b4ea5b65928a44c586 to your computer and use it in GitHub Desktop.
A stupid dynamic array,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Array<E> { | |
| public Unit[] arr; | |
| public int size; | |
| private int last_inserted = -1; | |
| private class Unit<E> { | |
| public E data; | |
| Unit(E thing) { | |
| this.data = thing; | |
| } | |
| } | |
| Array(int size) { | |
| this.size = size; | |
| this.arr = new Unit[this.size]; | |
| } | |
| public void append(E thing) { | |
| if (last_inserted + 1 >= size) { | |
| Unit[] temp_arr = new Unit[this.size * 2]; | |
| System.arraycopy(this.arr, 0, temp_arr, 0, this.size); | |
| this.arr = temp_arr; | |
| this.size *= 2; | |
| } | |
| last_inserted++; | |
| Unit u = new Unit(thing); | |
| this.arr[last_inserted] = u; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment