ArrayList zbz=
[[200204, 0, 3, 0, 0, 0, 0], [200205, 0, 0, 0, 3, 0, 0], [200205, 0, 1, 0, 0, 0, 0], [200405, 0, 2, 0, 0, 0, 0], [200405, 0, 0, 0, 0, 0, 1], [200406, 0, 3, 0, 0, 0, 0], [200406, 0, 0, 0, 0, 0, 1], [200407, 0, 2, 0, 0, 0, 0]];
需要把第一列相同的组合到一起。组合成:
[[[200204, 0, 3, 0, 0, 0, 0], [200205, 0, 1, 0, 3, 0, 0], [200405, 0, 2, 0, 0, 0, 1],
[200406, 0, 3, 0, 0, 0, 1], [200407, 0, 2, 0, 0, 0, 0] ];
不知道有什么好的方法吗?
[[200204, 0, 3, 0, 0, 0, 0], [200205, 0, 0, 0, 3, 0, 0], [200205, 0, 1, 0, 0, 0, 0], [200405, 0, 2, 0, 0, 0, 0], [200405, 0, 0, 0, 0, 0, 1], [200406, 0, 3, 0, 0, 0, 0], [200406, 0, 0, 0, 0, 0, 1], [200407, 0, 2, 0, 0, 0, 0]];
需要把第一列相同的组合到一起。组合成:
[[[200204, 0, 3, 0, 0, 0, 0], [200205, 0, 1, 0, 3, 0, 0], [200405, 0, 2, 0, 0, 0, 1],
[200406, 0, 3, 0, 0, 0, 1], [200407, 0, 2, 0, 0, 0, 0] ];
不知道有什么好的方法吗?
{200205, 0, 1, 0, 0, 0, 0}, {200405, 0, 2, 0, 0, 0, 0}, {200405, 0, 0, 0, 0, 0, 1},
{200406, 0, 3, 0, 0, 0, 0}, {200406, 0, 0, 0, 0, 0, 1}, {200407, 0, 2, 0, 0, 0, 0}};
int nRow = rect.length;
int nCol = rect[0].length;
int[][]resultRect = new int [nRow][nCol];
for (int i = 0 ;i < nRow ; ++i){
for (int j=0 ; j <nCol ; ++j) {
resultRect[i][j] = rect[i][j];
}
}
int[] nNewLine = new int[nRow];
for (int i = 0 ;i < nRow ; ++i){
for (int j=i+1 ; j <nCol ; ++j) {
if (rect[i][1] == rect[j][1]){
nNewLine = mergeLine(rect , i , j);
deleteLine(resultRect , i , j);
insertLine(resultRect ,nNewLine);
}
}
}
int[] mergeLine (int[][]rect , int i , int j){
final int nLen = rect.length;
int[] result = new int[nLen];
for (int k = 0 ; k < nLen ; ++k){
result[k] = rect[i][k]+rect[j][k];
}
return result;
}
void deleteLine (int[][]resultRect , int i , int j){
final int nLen = resultRect.length;
for (int k = i ; k < nLen-1 ; ++k){
resultRect[k] = resultRect [k+1];
}
resultRect[nLen-1] = null;
if (i<j){
--j;
}
for (int k = j ; k < nLen-2 ; ++k){
resultRect[k] = resultRect [k+1];
}
resultRect[nLen-2] = null;
}
void insertLine (int[][]resultRect , int[] nNewLine ){
final int nLen = resultRect.length;
int i = 0;
for ( ;i<nLen ; ++i)
if (null == resultRect[i]){
break;
}
for (int j =0 ; j < nNewLine.length ; ++j){
resultRect[i][j] = nNewLine[j];
}
}