#Is there a way to remove an element at a specific index from an array?
13 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @rocky cosmos! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Arrays are fixed size, so you can’t “remove” elements from them. If you have to remove an element, you have to create a new array that is one element smaller than the previous array, and shift all the elements after the deleted one to the left by 1.
So given [1,2,3,4,5], if we want to remove 3, we must create a new array of length 4, and shift 4 and 5 to the left by one, so we get [1,2,4,5]
i see, so lets say i dont know what the size of the final array is
do i create an array with a length of 100 aka oversized array?
More expensive of an operation but you could also convert to/from lists and arrays
i thought there arent lists in java
Well I wouldn’t make an oversized array, your new array will either be the same size or smaller as this previous array
oh right
There are lists, they are like array but a dynamic size, so you can remove items etc. most common list is an ArrayList
i havent learned that yet so i dont think we are allowed to use it
Since it’s a 2D list, you could do it row by row, maybe go through the row, keep track of the indices that need to be removed, then create a new array with those indices gone, and make that the new row
hm okay let me try that