/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class CombineData {
public ArrayList combineColumns = new ArrayList();//ç¨äºä¿åéè¦åå¹¶çåå·
private String[][] datas;//tableçæ°æ®ï¼ç¨æ¥è®¡ç®åå¹¶çåå
æ ¼
private ArrayList> rowPoss;
private ArrayList> rowCounts;
public CombineData() { }
public CombineData(String[][] datas, int... combineColumns) {
this.datas = datas;
for (int i = 0; i < combineColumns.length; i++) {
if (combineColumns[i] < 0) {
continue; }
this.combineColumns.add(combineColumns[i]);
}
process();
}
public CombineData(String[][] datas, List combineColumns) {
this.datas = datas;
for (Integer column : combineColumns) {
if (column < 0) {
continue; }
this.combineColumns.add(column);
}
process();
}
public void initData(String[][] datas, int... combineColumns) {
this.datas = datas;
this.combineColumns.clear();
for (int i = 0; i < combineColumns.length; i++) {
if (combineColumns[i] < 0) {
continue;
}
this.combineColumns.add(combineColumns[i]);
}
process();
}
public void initData(String[][] datas, List combineColumns) {
this.datas = datas;
this.combineColumns.clear();
for (Integer column : combineColumns) {
if (column < 0) {
continue;
}
this.combineColumns.add(column);
}
process();
}
private void process() {
rowPoss = new ArrayList>();
rowCounts = new ArrayList>();
for (Integer integer : combineColumns) {
HashMap rowPos = new HashMap();
HashMap rowCount = new HashMap();
String pre = "";
int count = 0;
int start = 0;
for (int i = 0; i < datas.length; i++) {
String[] data = datas[i];
if (pre.equals(data[integer])) {
count++;
} else {
rowCount.put(start, count);
pre = data[integer];
count = 1;
start = i;
}
rowPos.put(i, start);
}
rowCount.put(start, count);
rowPoss.add(rowPos);
rowCounts.add(rowCount);
}
}
/**
* è¿åtableä¸rowè¡columnååå
æ ¼æè·¨è¡æ°
*/
public int span(int row, int column) {
int index = combineColumns.lastIndexOf(column);
if (index != -1) {
return rowCounts.get(index).get(rowPoss.get(index).get(row));
} else {
return 1;
}
}
/**
* è¿åtableä¸rowè¡columnååå
æ ¼æå¨çåå¹¶åå
æ ¼çèµ·å§è¡ä½ç½®
*/
public int visibleCell(int row, int column) {
int index = combineColumns.lastIndexOf(column);
if ((index != -1) && row > -1) {
return rowPoss.get(index).get(row);
} else {
return row;
}
}
}