java int数组中 移除指定重复的数字

比如
int [] coun=new int{1,1,1,2,2,3,3,3,4,4,5,5,7,7}
求写个这样的方法
public int removeCu(int a){
//通过指定数字a删除它
//比如这个数字为3
}
结果为
for(int i=0;i<coun.length;i++){
System.out.print(coun[i]+" ");
}
1 1 1 2 2 4 4 5 5 7 7

注意a不是索引是值,还有别把这个数隐藏了 谢谢了 急用!用什么方法都行 只要最后变成int型数组
忘打了一个[] 数组

上段粗糙的代码:

public static int[] removeCu(int a) {
// 通过指定数字a删除它
// 比如这个数字为3
ArrayList lst = new ArrayList();
for (int i = 0; i < coun.length; i++) {
if (coun[i] != a) {
lst.add(coun[i]);
}
}
int[] rs = new int[lst != null ? lst.size() : 0];
for (int j = 0; j < rs.length; j++) {
rs[j] = Integer.parseInt(lst.get(j).toString());
}
System.out.println(Arrays.toString(rs));

return rs;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-03
上段粗糙的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

public static int[] removeCu(int a) {
// 通过指定数字a删除它
// 比如这个数字为3
ArrayList lst = new ArrayList();
for (int i = 0; i < coun.length; i++) {
if (coun[i] != a) {
lst.add(coun[i]);
}
}
int[] rs = new int[lst != null ? lst.size() : 0];
for (int j = 0; j < rs.length; j++) {
rs[j] = Integer.parseInt(lst.get(j).toString());
}
System.out.println(Arrays.toString(rs));

return rs;
}
第2个回答  2013-07-03
import java.util.ArrayList;
import java.util.Iterator;
public class Java0703 {
public static void main(String[] args) {
int [] coun={1,1,1,2,2,3,3,3,4,4,5,5,7,7};
ArrayList al = new ArrayList();
for (int i=0;i<coun.length; i++){
if (coun[i] != 3)
al.add(coun[i]);
}
Iterator it = al.iterator();
while(it.hasNext())
System.out.print(it.next()+" ");
}
}追问

那 怎么最后转换成int数组
非常感谢你的想法!

相似回答