是否有可能从一个阵列中删除项目? [英] Is it possible to remove items from a array?
问题描述
是一个数组(不是一个ArrayList),例如可以删除项目。
is it possible remove items from an array (not an arraylist)e.g.
JButton[] arr = {button1, button2, button3};
我想从中删除按钮1。
I want to remove button1 from it.
推荐答案
是的,你可以这样做:改编[0] = NULL;
和噗,BUTTON1是走了的从数组的
Yep, you could do: arr[0] = null;
, and poof, button1 is gone from the array.
如果要在阵列要小一些,比方说2项数组,那么你就需要通过做一个数组复制System.arraycopy(originalArray,1,destinationArray,0,2)
,复制过去的两个项目为两个项的Array。
If you want the array to be smaller, say a 2 item array, then you'll need to do an array copy via System.arraycopy(originalArray, 1, destinationArray, 0, 2)
, copying the last two items into a two-item array.
最好的解决办法:只使用一个弗利'的ArrayList的,这就是它的建成为
The best solution: just use a flippin' ArrayList as that's what it's built for.
注顺便说一句,你的问题无关与Swing。
Note as an aside, your question has nothing to do with Swing.
这篇关于是否有可能从一个阵列中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!