Created
September 30, 2025 19:52
-
-
Save MrKomodoDragon/345bbf030d66bf86506f01b87b2f17b9 to your computer and use it in GitHub Desktop.
Array with syntax highlighting
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