永发信息网

JAVA编程:把一个数组的元素复制到另个数组;去除重复元素不能用SET集合;每次复制的记录输到一个文件里

答案:1  悬赏:80  手机版
解决时间 2021-07-30 21:28
JAVA编程:把一个数组的元素复制到另个数组;去除重复元素不能用SET集合;每次复制的记录输到一个文件里
最佳答案
package com.ajax.test;

import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;


public class CopyArrayAndRemoveDuplicate {

private static String FILE_PATH = "d:/abc.txt";
private static File file;

static {
file = new File(FILE_PATH);
}


private int[] removeDuplicate(int[] nums) throws Exception {
int[] tmpArray = new int[nums.length];
int count = 0;

loop: //
for (int i = 0; i < nums.length; i++) {
int tmp = nums[i];

for (int j = 0; j < count; j++) {
if (tmp == tmpArray[j])
continue loop;
}
tmpArray[count++] = tmp;
log("成功复制了元素" + tmp);// 写日志
}
return copyArray(tmpArray, 0, count);
}


private int[] copyArray(int[] srcArray, int startIndex, int endIndex)
throws Exception {
if (endIndex <= startIndex)
throw new Exception("Argumens wrong!");

int[] desArray = new int[endIndex - startIndex];

System.arraycopy(srcArray, startIndex, desArray, 0, desArray.length);
return desArray;
}


private void log(String oprate) {
FileWriter out = null;

try {
out = new FileWriter(file, true);
out.write(oprate + "\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
out = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

public static void main(String[] args) {
int[] nums = { 1, 223, 1, 2, 2, 2, 3, 2, 3, 34, 45, 5, 5, 3, 23, 2, 2,
3, 4, 5, 5, 6, 7, 78, 8, 9, 34, 90, 45, 675, 4, };

int[] finalArray;

try {
finalArray = new CopyArrayAndRemoveDuplicate()
.removeDuplicate(nums);

System.out.println(Arrays.toString(finalArray));
} catch (Exception e) {
e.printStackTrace();
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
安踏的特点
谁帮我把游戏人生图标开下
DNF炎息多少钱?
汉阳区武汉聚福轩地址在哪里啊
专家请快进,越快越好,急!
问问qq怎么升级快
为什么我的宽带总是自动断开
dnf 回血道具 最多
平房区哈尔滨丰园美食山庄地址在哪里啊
怎么写文献综述,文献综述 英语怎么说
谁能找回遗失的心?
谁有歌曲mv下载网址或论坛
同桌太吵,太烦!
N86和E72比哪个还一点
西峡县南阳西峡县辉瑞劳务派遣有限公司地址在
推荐资讯
石家庄到宁晋车费多少
我梦幻一个20每天上1小时,杂升级最快,大概
张杰量身定做的歌词
完美世界国际版多少封顶
诺基亚5530怎样下载09版QQ
语文仿写句子 急!
柳州到北京的火车要多长时间到达
男人会等一个女人多久?
今天篮球那队比赛
请问电脑怎么回事
曾都区随州市中医院怎么去啊,谁知道地址啊
联通的怎么开通绿钻!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?