#Is there a way to remove an element at a specific index from an array?

13 messages · Page 1 of 1 (latest)

fervent otterBOT
#

This post has been reserved for your question.

Hey @rocky cosmos! Please use /close or the Close Post button 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.

earnest briar
#

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]

rocky cosmos
#

do i create an array with a length of 100 aka oversized array?

earnest briar
#

More expensive of an operation but you could also convert to/from lists and arrays

rocky cosmos
#

i thought there arent lists in java

earnest briar
#

Well I wouldn’t make an oversized array, your new array will either be the same size or smaller as this previous array

rocky cosmos
#

oh right

earnest briar
rocky cosmos
#

i havent learned that yet so i dont think we are allowed to use it

earnest briar
#

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

rocky cosmos
#

hm okay let me try that