java中去除某个数值,其他存入新数组怎么做?

如题所述

第1个回答  2022-11-16

数组是没法删除其中的元素的,你在定义数组的时候,长度已经给定义好了,你可以采取中间变量,把数组的内容重新读一遍,需要移除的不需要读。

或者用arrylist arrylist的长度可以改变,数组的定义好了,是不可以改变的。

去除之后数据长度变短可以么?可以的话就用如下代码:

List<Integer> tempList = new ArrayList();fr(int n:a){if(n!=0){tempList.add(n);}}Integer[] newArr = tempList.toArray(new Inte

相似回答