题目需要做的有2个功能
1 根据一个字符串数组 求出连续且相同的数组的起始和结束位置
2 b参考数组a,a数组中的相同且连续的字符串如"hi"的起始和结束位置分别为x,y那么就在数组b中求出x-y中的相同且连续的字符的起始和结束位置(数组a中相等且连续的字符可能很多,a中的一个x-y区间 对应在b中也可能有多个相同且连续的字符)。根据a相等且连续的区间求出b中相等且连续的字符的起始和结束位置。
这个基础的算法,奠定了java对excel合并单元格的思想。
public static void main(String[] args) {
int[] lon = new int[] {1,1,1,3,4,5,6,6,6,7,88,88,99};
int b = 0;
int start = 0;
int end = 0;
for (int i = 0; i < lon.length; i++) {
if (b != lon[i] && b != 0) {
if(start==(i-1)){
System.out.println("数字 "+ b + " 起始位置和结束位置相同("+start+")");
}else{
System.out.println("数字 "+ b + " 起始位置为 " + start + " 结束位置为 "+ (i-1));
}
}
end = i;
if (b != lon[i]){
b = lon[i];
start = i;
}
if (end == lon.length - 1) {
if(start==i){
System.out.println("数字 "+ b + " 起始位置和结束位置相同("+start+")");
}else{
System.out.println("数字 "+b+ " 起始位置为 " + start + " 结束位置为 " + i);
}
}
}
}
修改完整:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MergeUtil {
public Map getMixture(int referStart,int referEnd,int toStart,int toEnd){
Map map = null;
if(!(referEnd < toStart) && !(referStart > toEnd)){ // 判断是否有交集
map = new HashMap();
if(referStart>=toStart){// 如果有交集最小值取最大值
map.put("min", referStart);
}else{
map.put("min", toStart);
}
if(referEnd>=toEnd){ //如果有交集最大值取最小值
map.put("max", toEnd);
}else{
map.put("max", referEnd);
}
}
return map;
}
/*
* 无参照
*/
public Map getColumn(String[] comlumnData,String startkey, String endkey) {
Map map = new HashMap();
String temp = "^&*";
int start = 0;
int end = 0;
for (int i = 0; i < comlumnData.length; i++) {
if (i>0){
if (i < comlumnData.length - 1 && temp.equals(comlumnData[i - 1]) && !temp.equals(comlumnData[i])) {
start = i;
} else {
if (!temp.equals(comlumnData[i])) { // 最后一个
start = i;
}
}
end = i;
}
temp = comlumnData[i];
map.put(startkey + comlumnData[i], new ExportBean(startkey + comlumnData[i], start, end));
}
return map;
}
/*
* 有参照列
*/
public Map referenceToColumn(String[] reference,String[] to,String referenceStart,String referenceEnd,String toStart,String toEnd) {
Map temp = new HashMap();
Map referenceMap = getColumn(reference,referenceStart,referenceEnd);
Map toMap = getColumn(to,toStart,toEnd );
Iterator referenceIter = referenceMap.entrySet().iterator();
Iterator toIter = toMap.entrySet().iterator();
while (referenceIter.hasNext()) {
Map.Entry referBeanEntry = (Map.Entry) referenceIter.next();
ExportBean referBean = (ExportBean) referBeanEntry.getValue();
while (toIter.hasNext()) {
Map.Entry toBeanEntry = (Map.Entry) toIter.next();
ExportBean toBean = (ExportBean) toBeanEntry.getValue();
Map mixture = getMixture(referBean.getStart(),referBean.getEnd(), toBean.getStart(), toBean.getEnd());
if(mixture!=null && mixture.get("min")!=mixture.get("max")){
toBean.setStart((Integer)mixture.get("min"));
toBean.setEnd((Integer)mixture.get("max"));
temp.put(toBean.getKeyname(), toBean);
System.out.println("有参照的列------" + toBean.getKeyname() + "--------"+ toBean.getStart() + "----------" + toBean.getEnd());
}
/* if(toBean.getStart()>=referBean.getStart()&& toBean.getEnd()<=referBean.getEnd() && !(toBean.getStart()==toBean.getEnd())){
System.out.println("有参照的列------" + toBean.getKeyname() + "--------"+ toBean.getStart() + "----------" + toBean.getEnd());
dosomething
temp.put(toBean.getKeyname(), toBean);
}*/
}
toIter = toMap.entrySet().iterator();
}
return temp;
}
public static void main(String[] args) {
/* String[] reference = new String[]{"1","1","1","1","1","1","1","1","1","2"};// 参考列
String[] to = new String[]{"1","1","1","2","2","5","9","9","a","a"};
*/
String[] reference = new String[]{"时间","时间","时间","时间","时间","时间","时间","时间2","时间2","时间2"};// 参考列
String[] to = new String[]{"时1","时1","时2","时3","时5","时5","时6","时6","时6","时6"};
MergeUtil m = new MergeUtil();
// 参照列测试
Map mmm = m.referenceToColumn(reference, to, "keyplaceIdStart", "keyplaceIdEnd", "projectStart","projectEnd");
// 无参照咧测试
Map referenceMap = m.getColumn(reference, "cStart", "cEnd");
Iterator referenceIter = referenceMap.entrySet().iterator();
while (referenceIter.hasNext()) {
Map.Entry referBeanEntry = (Map.Entry) referenceIter.next();
ExportBean referBean = (ExportBean) referBeanEntry.getValue();
System.out.println("无参照的列: "+ referBean.getKeyname()+" "+ referBean.getStart()+ " "+ referBean.getEnd());
}
}
}
class ExportBean {
private String keyname;
private int start = 0;
private int end = 0;
public ExportBean(String keyname, int start, int end) {
this.keyname = keyname;
this.start = start;
this.end = end;
}
public String getKeyname() {
return keyname;
}
public void setKeyname(String keyname) {
this.keyname = keyname;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
}
下面再次修改如下,现在看着清爽很多而且还去掉了一些bug。
public class MergeUtils {
int start = 0;
public List getColumn(List list,String[] comlumnData) {
for (int i = 0; i < comlumnData.length; i++) {
if (i>0 && !comlumnData[i-1].equals(comlumnData[i])) // 当前值与上一个值不同
start = i;
if(start!=i && !(i<(comlumnData.length-1) && comlumnData[i].equals(comlumnData[i+1])))
list.add(new Export(start,i));
}
return list;
}
/*
* 有参照列
*/
public List referenceToColumn(String[] reference, String[] to) {
String[] comlumnData = new String[to.length];
for(int i=0;i<comlumnData.length;i++)
comlumnData[i] = reference[i]+to[i];
return getColumn(new ArrayList(),comlumnData);
}
public static void main(String[] args) {
String[] reference = new String[] { "时间", "时间", "时间","时间","时间","时间", "时间", "时间2","时间2","时间" };// 参考列
String[] to = new String[] { "时1", "时1", "时2", "时3", "时5", "时5", "时6","时6", "时6", "时7" };
MergeUtils m = new MergeUtils();
List mmm = m.referenceToColumn(reference, to);
Iterator ite = mmm.iterator();
while(ite.hasNext()){
Export ex = (Export)ite.next();
System.out.println("开始:" + ex.getStart() + " 结束:" + ex.getEnd());
}
}
}
class Export {
private int start = 0;
private int end = 0;
public Export(int start, int end) {
this.start = start;
this.end = end;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
}