发布网友 发布时间:2022-04-22 21:47
共4个回答
热心网友 时间:2023-10-03 10:14
package com.ajax.test;
import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
/**
* 把一个数组的元素复制到另个数组; 去除重复元素不能用SET集合; 每次复制的记录输到一个文件里
*
* @author ajax_2003
* @version 1.0, 2009-7-26
*
*/
public class CopyArrayAndRemoveDuplicate {
private static String FILE_PATH = "d:/abc.txt";
private static File file;
static {
file = new File(FILE_PATH);
}
/**
* 取出冗余数据
*
* @param nums
* 原数组
*/
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);
}
/**
* 拷贝数组
*
* @param srcArray
* 要拷贝的数组
* @param startIndex
* 拷贝起始索引
* @param endIndex
* 拷贝结束索引
* @return 结果数组
*/
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;
}
/**
* 输出操作日志(即: 每次复制的记录输到一个文件里)
*
* @param oprate
*/
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();
}
}
}
热心网友 时间:2023-10-03 10:14
public static void main(String[] args) {
String[] str1 = {"a","b","c"};
String[] str2 = {"a","e","f"};
List<String> listStr1 = new ArrayList<String>(Arrays.asList(str2));
List<String> listStr2 = new ArrayList<String>(Arrays.asList(str2));
String oldStr = Arrays.toString(str1);
for(String str:listStr1){
if(oldStr.contains(str)){
listStr2.remove(str);
}
}
Object[] str3 = listStr2.toArray();
String[] newStr = Arrays.copyOf(str1, str1.length + str3.length);
System.arraycopy(str3, 0, newStr, str2.length, str3.length);
System.out.println(Arrays.toString(newStr));
}
热心网友 时间:2023-10-03 10:15
int len=arrA.length;
int arrB[]=new int[len];
int count=0;
for(int i=0; i<len; i++){
boolean isFound=false;
for(int j=0; j<=cound; j++){
if(arrB[j]==arrA[i]){
isFound=true;
break;
}
}
if(!isFound){
arrB[count]=arrA[i];
count++;
}
}
//FileOutputStream 写文件
热心网友 时间:2023-10-03 10:16
int len=arrA.length;
int arrB[]=new int[len];
int count=0;
boolean flag;
for(int i=0; i<len; i++) {
flag = false;
for(int j=0; j<=count; j++) {
if(arrB[j]==arrA[i]) {
flag = true; // 标识元素重复
break;
}
}
if(!flag){ // 表示当前元素在数组中不存在,即无重复。
arrB[count]=arrA[i];
count++;
}
}
// io流
// 代码是copy楼上还是楼下的来改的。。。。。。嘿嘿