JAVA编程:把一个数组的元素复制到另个数组;去除重复元素不能用SET集合;每次复制的记录输到一个文件里
答案:1 悬赏:80 手机版
解决时间 2021-07-30 21:28
- 提问者网友:我是我
- 2021-07-30 18:00
JAVA编程:把一个数组的元素复制到另个数组;去除重复元素不能用SET集合;每次复制的记录输到一个文件里
最佳答案
- 五星知识达人网友:玩世
- 2021-07-30 18:17
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();
}
}
}
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();
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯