如何将一个数组中相同的数字去除 ice_csdn()的方法可能不好。把int打包成Integer要花很多时间,对于小数组,重复几率大的数组,这种方法的效率会很差。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class DeleteSameItemInArray{ public int[] getNewArr(int[] intArr){ int[] newArr = new int[intArr.length]; boolean itemExist = false, zeroHasBeenPutIn = false; int k = 0, actualLength = 0; for(int i = 0; i < intArr.length; ++i){ for(int j = 0; j < newArr.length; ++j){ if (intArr[i] != 0){ if (intArr[i] == newArr[j]){ itemExist = true; break; }else{ itemExist = false; } }else{ if (!zeroHasBeenPutIn) { itemExist = false; }else{ itemExist = true; } } } if (!itemExist){ newArr[k] = intArr[i]; if (intArr[i] == 0) zeroHasBeenPutIn = true; k++; actualLength++; itemExist = true; } } int[] actualNewArr = new int[actualLength]; System.arraycopy(newArr, 0, actualNewArr, 0, actualLength); return actualNewArr; } public static void main(String[] args){ int[] arr = new DeleteSameItemInArray().getNewArr(new int[]{1,5,8,1,5,2}); for(int k = 0; k < arr.length; ++k){ System.out.println("index " + k + ": " + arr[k]); } }} 不好意思,刚才忘记格式化一下了。欢迎拍砖! ^_^public class DeleteSameItemInArray{ public int[] getNewArr(int[] intArr){ int[] newArr = new int[intArr.length]; boolean itemExist = false, zeroHasBeenPutIn = false; int k = 0, actualLength = 0; for(int i = 0; i < intArr.length; ++i){ for(int j = 0; j < newArr.length; ++j){ if (intArr[i] != 0){ if (intArr[i] == newArr[j]){ itemExist = true; break; }else{ itemExist = false; } }else{ if (!zeroHasBeenPutIn) { itemExist = false; }else{ itemExist = true; } } } if (!itemExist){ newArr[k] = intArr[i]; if (intArr[i] == 0) zeroHasBeenPutIn = true; k++; actualLength++; itemExist = true; } } int[] actualNewArr = new int[actualLength]; System.arraycopy(newArr, 0, actualNewArr, 0, actualLength); return actualNewArr; } public static void main(String[] args){ int[] arr = new DeleteSameItemInArray().getNewArr(new int[]{1,5,8,1,5,2}); for(int k = 0; k < arr.length; ++k){ System.out.println("index " + k + ": " + arr[k]); } }} to xiaohaiz(城里的老土,两眼依然通红!) 因为俺水平差,所以才“写出这么复杂的东西”。希望不会玷污你的眼睛啊!!呵呵。 TO yoic:千万别这么说这不是折杀俺了嘛复杂也未必是坏事啊其实二楼的建议就很方便了如果不考虑高效率的问题 如果用两重for循环,效率太低了。可以把给出的数当作一数组的下标计数,要走一遍;再走一遍,把非零的数组元素读出,即可达到目的,因为数组下标是不重复的,而且还是排好序的。时间复杂度为o(n),虽然空间多费了些,但用空间换时间,大家都懂了。如:{1,5,8,1,5,2}a[1]=2,a[2]=1,a[5]=2,a[8]=1,其余的都为0再一个循环后,就有{1,2,5,8}了。 to:boyard(叶落知秋) 如果是{1000,10000}岂不是要a[10000]? 将数组中的元素放到Set中,再从Set里取出来就可以了活用API是好习惯啊 哈希表的查找效率是非常高的,我写了个利用哈希表的算法,抛砖引玉:package net.yeah.jianfeng.exercises;import java.util.HashMap;public class Distill { int[] parse(int[] source) { HashMap hm = new HashMap(); int[] sorted = new int[source.length]; int count = 0; for(int i = 0 ; i < source.length ; i ++) { String key = "" + source[i]; if(hm.containsKey(key)) continue; hm.put(key, key); sorted[count] = source[i]; ++ count; } int[] rtn = new int[count]; for(int j = 0 ; j < count ; j ++) { rtn[j] = sorted[j]; } return rtn; } public static void main(String[] args) { Distill distill = new Distill(); int[] rtn = distill.parse(new int[] {10,2,4,6,5,7,1,11,99,10,11,9999,888,99}); for(int i = 0 ; i < rtn.length; i ++) { System.out.print(rtn[i] + " "); } }} 关于线程实现:如果等待10秒拿不到另一个资源,放弃当前资源 关于虚拟机不能关闭的问题 有难度的问题! 如何计算指定文件中包含的单词数目?? byte[]类型转换为String型后再转换回byte[],为什么不同?在线等,解决立刻给分 java中取子字符串的函数是什么呀? 如何把一个byte[]的内容以二进制写到文本文件中?? 求显示时钟的小程序~~ paint 方法和 paintComponent 方法有什么区别? 小白求救 希望大神们指点迷津 为什么switch(表达式) 中表达式不能为String 类型???? applet的问题~我没搞过,谁知道的。
public int[] getNewArr(int[] intArr){
int[] newArr = new int[intArr.length];
boolean itemExist = false,
zeroHasBeenPutIn = false; int k = 0,
actualLength = 0; for(int i = 0; i < intArr.length; ++i){
for(int j = 0; j < newArr.length; ++j){ if (intArr[i] != 0){
if (intArr[i] == newArr[j]){
itemExist = true;
break;
}else{
itemExist = false;
}
}else{
if (!zeroHasBeenPutIn) {
itemExist = false;
}else{
itemExist = true;
}
}
}
if (!itemExist){
newArr[k] = intArr[i];
if (intArr[i] == 0)
zeroHasBeenPutIn = true;
k++;
actualLength++;
itemExist = true;
}
}
int[] actualNewArr = new int[actualLength];
System.arraycopy(newArr, 0, actualNewArr, 0, actualLength);
return actualNewArr;
} public static void main(String[] args){
int[] arr = new DeleteSameItemInArray().getNewArr(new int[]{1,5,8,1,5,2});
for(int k = 0; k < arr.length; ++k){
System.out.println("index " + k + ": " + arr[k]);
}
}
}
public int[] getNewArr(int[] intArr){
int[] newArr = new int[intArr.length];
boolean itemExist = false, zeroHasBeenPutIn = false;
int k = 0, actualLength = 0; for(int i = 0; i < intArr.length; ++i){
for(int j = 0; j < newArr.length; ++j){
if (intArr[i] != 0){
if (intArr[i] == newArr[j]){
itemExist = true;
break;
}else{
itemExist = false;
}
}else{
if (!zeroHasBeenPutIn) {
itemExist = false;
}else{
itemExist = true;
}
}
}
if (!itemExist){
newArr[k] = intArr[i];
if (intArr[i] == 0)
zeroHasBeenPutIn = true;
k++;
actualLength++;
itemExist = true;
}
}
int[] actualNewArr = new int[actualLength];
System.arraycopy(newArr, 0, actualNewArr, 0, actualLength);
return actualNewArr;
} public static void main(String[] args){
int[] arr = new DeleteSameItemInArray().getNewArr(new int[]{1,5,8,1,5,2});
for(int k = 0; k < arr.length; ++k){
System.out.println("index " + k + ": " + arr[k]);
}
}
}
因为俺水平差,所以才“写出这么复杂的东西”。希望不会玷污你的眼睛啊!!呵呵。
千万别这么说这不是折杀俺了嘛复杂也未必是坏事啊
其实二楼的建议就很方便了如果不考虑高效率的问题
再走一遍,把非零的数组元素读出,即可达到目的,因为数组下标是不重复的,而且还是
排好序的。时间复杂度为o(n),虽然空间多费了些,但用空间换时间,大家都懂了。
如:{1,5,8,1,5,2}
a[1]=2,a[2]=1,a[5]=2,a[8]=1,其余的都为0
再一个循环后,就有{1,2,5,8}了。
如果是{1000,10000}
岂不是要a[10000]?
活用API是好习惯啊
HashMap hm = new HashMap();
int[] sorted = new int[source.length];
int count = 0; for(int i = 0 ; i < source.length ; i ++) {
String key = "" + source[i];
if(hm.containsKey(key))
continue; hm.put(key, key);
sorted[count] = source[i];
++ count;
} int[] rtn = new int[count];
for(int j = 0 ; j < count ; j ++) {
rtn[j] = sorted[j];
} return rtn;
} public static void main(String[] args) {
Distill distill = new Distill();
int[] rtn = distill.parse(new int[] {10,2,4,6,5,7,1,11,99,10,11,9999,888,99});
for(int i = 0 ; i < rtn.length; i ++) {
System.out.print(rtn[i] + " ");
}
}}