有如下的要求,数组import java.util.ArrayList;
import java.util.List;
public class test { /**
* @param args
*/
public static void main(String[] args) {
List<String[]> testDataList = new ArrayList<String[]>();
String[] testData1 = new String[]{"1","2","111","222","1"};
String[] testData2 = new String[]{"1","2","111","222","1"};
String[] testData3 = new String[]{"1","2","111","222","2"};
String[] testData7 = new String[]{"1","2","111","222","2"};
String[] testData8 = new String[]{"1","2","111","222","2"};
String[] testData4 = new String[]{"2","1","111","222","1"};
String[] testData5 = new String[]{"2","1","111","222","2"};
String[] testData6 = new String[]{"2","2","111","222","1"}; testDataList.add(testData1);
testDataList.add(testData2);
testDataList.add(testData3);
testDataList.add(testData4);
testDataList.add(testData5);
testDataList.add(testData6);
testDataList.add(testData7);
testDataList.add(testData8);
List<String[]> result1 = new ArrayList<String[]>();
for (int i = 0; i < testDataList.size(); i++){
String[] result = new String[6];
String[] listData0 = testDataList.get(i);
result[0] = listData0[0];
result[1] = listData0[1];
result[2] = listData0[2];
result[3] = listData0[3];
result[4] = "0";
result[5] = "0"; for (int j = testDataList.size()-1; j > i; j--) {
String[] listData1 = testDataList.get(j);
if (listData0[0] == listData1[0] && listData0[1] == listData1[1]){ if (listData0[4] == listData1[4] &&(listData1[4] == "1" || listData0[4] == "1")){
result[2] =
String.valueOf(Integer.parseInt(result[2]) + Integer.parseInt(listData0[2]));
result[3] =
String.valueOf(Integer.parseInt(result[3]) + Integer.parseInt(listData0[3]));
} else {
result[4] =
String.valueOf(Integer.parseInt(result[4]) + Integer.parseInt(listData1[2]));
result[5] =
String.valueOf(Integer.parseInt(result[5]) + Integer.parseInt(listData1[3]));
}
testDataList.remove(j);
}
}
result1.add(result);
}
for (int i = 0; i < result1.size(); i++) {
for (int j = 0; j < result1.get(i).length; j++){
System.out.print(result1.get(i)[j]+" ");
}
System.out.println();
}
}}
如果数组的第一位并且第二位相等的话,就将数组合成一条数据,并根据第五位数值进行判断,为1的时候将3,4所有的数值相加放在新生成的数组的3,4位,为2的时候将3,4所有的数值相加放在新生成的数组的5,6位,上面的程序我已经写好了,可是感觉比较复杂,有没有什么简单的方法?高手们帮帮忙啊
import java.util.List;
public class test { /**
* @param args
*/
public static void main(String[] args) {
List<String[]> testDataList = new ArrayList<String[]>();
String[] testData1 = new String[]{"1","2","111","222","1"};
String[] testData2 = new String[]{"1","2","111","222","1"};
String[] testData3 = new String[]{"1","2","111","222","2"};
String[] testData7 = new String[]{"1","2","111","222","2"};
String[] testData8 = new String[]{"1","2","111","222","2"};
String[] testData4 = new String[]{"2","1","111","222","1"};
String[] testData5 = new String[]{"2","1","111","222","2"};
String[] testData6 = new String[]{"2","2","111","222","1"}; testDataList.add(testData1);
testDataList.add(testData2);
testDataList.add(testData3);
testDataList.add(testData4);
testDataList.add(testData5);
testDataList.add(testData6);
testDataList.add(testData7);
testDataList.add(testData8);
List<String[]> result1 = new ArrayList<String[]>();
for (int i = 0; i < testDataList.size(); i++){
String[] result = new String[6];
String[] listData0 = testDataList.get(i);
result[0] = listData0[0];
result[1] = listData0[1];
result[2] = listData0[2];
result[3] = listData0[3];
result[4] = "0";
result[5] = "0"; for (int j = testDataList.size()-1; j > i; j--) {
String[] listData1 = testDataList.get(j);
if (listData0[0] == listData1[0] && listData0[1] == listData1[1]){ if (listData0[4] == listData1[4] &&(listData1[4] == "1" || listData0[4] == "1")){
result[2] =
String.valueOf(Integer.parseInt(result[2]) + Integer.parseInt(listData0[2]));
result[3] =
String.valueOf(Integer.parseInt(result[3]) + Integer.parseInt(listData0[3]));
} else {
result[4] =
String.valueOf(Integer.parseInt(result[4]) + Integer.parseInt(listData1[2]));
result[5] =
String.valueOf(Integer.parseInt(result[5]) + Integer.parseInt(listData1[3]));
}
testDataList.remove(j);
}
}
result1.add(result);
}
for (int i = 0; i < result1.size(); i++) {
for (int j = 0; j < result1.get(i).length; j++){
System.out.print(result1.get(i)[j]+" ");
}
System.out.println();
}
}}
如果数组的第一位并且第二位相等的话,就将数组合成一条数据,并根据第五位数值进行判断,为1的时候将3,4所有的数值相加放在新生成的数组的3,4位,为2的时候将3,4所有的数值相加放在新生成的数组的5,6位,上面的程序我已经写好了,可是感觉比较复杂,有没有什么简单的方法?高手们帮帮忙啊
static int[] targetArray;
public static void operationArray (int[] intArray, int[] targetArray){
if (intArray[4] == 1){
targetArray = new int[5]; targetArray[2] = targetArray[3] = intArray[2] + intArray[3];
}
else if (intArray[4] == 2){
targetArray = new int[7];
targetArray[4] = targetArray[5] = intArray[2] + intArray[3];
}
if (intArray[0] == intArray[1])
targetArray[0] = intArray[0];
for (int i=0; i<targetArray.length; i++)
System.out.print(targetArray[i] +", ");
}
public static void main(String args[]){
int[] array1 = new int[]{4, 5, 23, 47, 1};
int[] array2 = new int[]{4, 5, 23, 47, 2};
int[] array3 = new int[]{4, 4, 23, 47, 1};
int[] array4 = new int[]{4, 4, 23, 47, 2};
operationArray(array1,targetArray);
System.out.println();
operationArray(array2,targetArray);
System.out.println();
operationArray(array3,targetArray);
System.out.println();
operationArray(array4,targetArray);
System.out.println();
}
}
结果:
0, 0, 70, 70, 0,
0, 0, 0, 0, 70, 70, 0,
4, 0, 70, 70, 0,
4, 0, 0, 0, 70, 70, 0,
String[] testData1 = new String[]{"1","2","111","222","1"};
String[] testData2 = new String[]{"1","2","111","222","1"};
String[] testData3 = new String[]{"1","2","111","222","2"};
String[] testData7 = new String[]{"1","2","111","222","2"};
String[] testData8 = new String[]{"1","2","111","222","2"};
String[] testData4 = new String[]{"2","1","111","222","1"};
String[] testData5 = new String[]{"2","1","111","222","2"};
String[] testData6 = new String[]{"2","2","111","222","1"};运行出来的结果
1 2 222 444 333 666
2 1 111 222 111 222
2 2 111 222 0 0 就是当1,2位相同的时候,将相同的数据合并成一条,在根据5位的值,如果相同的话,第3位所有的数据相加,
第4位所有的数据相加,第5位的值为1的时候,将相加所得到的值放在新生成的数组的3,4位,第5位的值为2的时候,将相加所得到的值放在新生成的数组的5,6位
package com.qware;import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class TestApp { public static void main(String[] args) throws Exception {
String[] testData1 = new String[] { "1", "2", "111", "222", "1" };
String[] testData2 = new String[] { "1", "2", "111", "222", "1" };
String[] testData3 = new String[] { "1", "2", "111", "222", "2" };
String[] testData7 = new String[] { "1", "2", "111", "222", "2" };
String[] testData8 = new String[] { "1", "2", "111", "222", "2" };
String[] testData4 = new String[] { "2", "1", "111", "222", "1" };
String[] testData5 = new String[] { "2", "1", "111", "222", "2" };
String[] testData6 = new String[] { "2", "2", "111", "222", "1" }; List<String[]> list = new ArrayList<String[]>();
list.add(testData1);
list.add(testData2);
list.add(testData3);
list.add(testData4);
list.add(testData5);
list.add(testData6);
list.add(testData7);
list.add(testData8); StringBuffer s = new StringBuffer();
for (String[] arr : list) {
s.append(Arrays.toString(arr));
} Map<String, String[]> map = new HashMap<String, String[]>();
Pattern pattern = Pattern.compile("\\[.*?\\]");
Matcher m = pattern.matcher(s);
while (m.find()) {
pattern = Pattern.compile("(\\d*, ){2}(?=.*\\]$)");
Matcher m1 = pattern.matcher(m.group());
if (m1.find()) {
String[] temp = map.get(m1.group());
String[] nArr = m.group().substring(1).split("(, )|\\]");
if (null == temp) {
temp = new String[6];
Arrays.fill(temp, "0");
System.arraycopy(nArr, 0, temp, 0, 4);
} else {
join(temp, nArr);
}
map.put(m1.group(), temp);
} }
for (String[] arr : map.values()) {
System.out.println(Arrays.toString(arr));
} } private static void join(String[] dest, String[] src) {
if (Integer.parseInt(src[4]) == 1) {
dest[2] = String.valueOf(Integer.parseInt(dest[2])
+ Integer.parseInt(src[2]));
dest[3] = String.valueOf(Integer.parseInt(dest[3])
+ Integer.parseInt(src[3]));
} else if (Integer.parseInt(src[4]) == 2) {
dest[4] = String.valueOf(Integer.parseInt(dest[4])
+ Integer.parseInt(src[2]));
dest[5] = String.valueOf(Integer.parseInt(dest[5])
+ Integer.parseInt(src[3]));
}
}}输出结果:
[2, 2, 111, 222, 0, 0]
[1, 2, 222, 444, 333, 666]
[2, 1, 111, 222, 111, 222]
public static void main(String[] args) {
String[][] strArrs = {{"1","2","111","222","1"},
{"1","2","111","222","1"},
{"1","2","111","222","2"},
{"1","2","111","222","2"},
{"1","2","111","222","2"},
{"2","1","111","222","1"},
{"2","1","111","222","2"},
{"2","2","111","222","1"}};
//根据1,2,5位分类
HashMap hmValues = new HashMap();
for (int i = 0; i < strArrs.length; i++) {
String[] strLoop = strArrs[i];
String strKey = strLoop[0] + " " + strLoop[1];
HashMap hmLoop = hmValues.containsKey(strKey) ? (HashMap)hmValues.get(strKey) : new HashMap();
String[] strSubArr = new String[2];
System.arraycopy(strLoop, 2, strSubArr, 0, 2);
if (hmLoop.size() != 0) {
if (hmLoop.containsKey(strLoop[4])) {
((ArrayList)hmLoop.get(strLoop[4])).add(strSubArr);
} else {
ArrayList alstLoop = new ArrayList();
alstLoop.add(strSubArr);
hmLoop.put(strLoop[4], alstLoop);
}
}
if (hmLoop.size() == 0) {
ArrayList alstLoop = new ArrayList();
alstLoop.add(strSubArr);
hmLoop.put(strLoop[4], alstLoop);
hmValues.put(strKey, hmLoop);
}
}
String[][] strArrsValue = new String[hmValues.size()][6];
int i = 0;
for (Iterator it = hmValues.keySet().iterator(); it.hasNext(); i++) {
String key = (String)it.next();
String[] strKey = key.split(" ");
System.arraycopy(strKey, 0, strArrsValue[i], 0, 2);
HashMap hmLoop = (HashMap)hmValues.get(key);
if (hmLoop.containsKey("1")) {
String[] strLoop = getSum((ArrayList)hmLoop.get("1"));
System.arraycopy(strLoop, 0, strArrsValue[i], 2, 2);
}
if (hmLoop.containsKey("2")) {
String[] strLoop = getSum((ArrayList)hmLoop.get("2"));
System.arraycopy(strLoop, 0, strArrsValue[i], 4, 2);
}
for (int j = 0; j < 6; j++) {
strArrsValue[i][j] = strArrsValue[i][j] == null ? "0" : strArrsValue[i][j];
}
}
}
public static String[] getSum(ArrayList value) {
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < value.size(); i++) {
String[] strLoop = (String[])value.get(i);
sum1 += Integer.parseInt(strLoop[0]);
sum2 += Integer.parseInt(strLoop[1]);
}
return new String[]{Integer.toString(sum1), Integer.toString(sum2)};
}
}测试可得到结果
1 2 222 444 333 666
2 1 111 222 111 222
2 2 111 222 0 0
package com.qware;import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class TestApp { public static void main(String[] args) throws Exception {
String[] testData1 = new String[] { "1", "2", "111", "222", "1" };
String[] testData2 = new String[] { "1", "2", "111", "222", "1" };
String[] testData3 = new String[] { "1", "2", "111", "222", "2" };
String[] testData7 = new String[] { "1", "2", "111", "222", "2" };
String[] testData8 = new String[] { "1", "2", "111", "222", "2" };
String[] testData4 = new String[] { "2", "1", "111", "222", "1" };
String[] testData5 = new String[] { "2", "1", "111", "222", "2" };
String[] testData6 = new String[] { "2", "2", "111", "222", "1" }; List<String[]> list = new ArrayList<String[]>();
list.add(testData1);
list.add(testData2);
list.add(testData3);
list.add(testData4);
list.add(testData5);
list.add(testData6);
list.add(testData7);
list.add(testData8); StringBuffer s = new StringBuffer();
for (String[] arr : list) {
s.append(Arrays.toString(arr));
} Map<String, String[]> map = new HashMap<String, String[]>();
Pattern pattern = Pattern.compile("\\[.*?\\]");
Matcher m = pattern.matcher(s);
while (m.find()) {
pattern = Pattern.compile("(\\d*, ){2}(?=.*\\]$)");
Matcher m1 = pattern.matcher(m.group());
if (m1.find()) {
String[] temp = map.get(m1.group());
String[] nArr = m.group().substring(1).split("(, )|\\]");
if (null == temp) {
temp = new String[6];
Arrays.fill(temp, "0");
System.arraycopy(nArr, 0, temp, 0, 4);
} else {
join(temp, nArr);
}
map.put(m1.group(), temp);
} }
for (String[] arr : map.values()) {
System.out.println(Arrays.toString(arr));
} } private static void join(String[] dest, String[] src) {
if (Integer.parseInt(src[4]) == 1) {
dest[2] = String.valueOf(Integer.parseInt(dest[2])
+ Integer.parseInt(src[2]));
dest[3] = String.valueOf(Integer.parseInt(dest[3])
+ Integer.parseInt(src[3]));
} else if (Integer.parseInt(src[4]) == 2) {
dest[4] = String.valueOf(Integer.parseInt(dest[4])
+ Integer.parseInt(src[2]));
dest[5] = String.valueOf(Integer.parseInt(dest[5])
+ Integer.parseInt(src[3]));
}
}}