今天写工具类时所犯的错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了今天写工具类时所犯的错误相关的知识,希望对你有一定的参考价值。
很感谢公司的大神,不厌其烦的进行指导,在大神的帮助下终于写完了。。
写一个将时间段转换取并集合并的一个工具类,写了四五次,醉了
第一次代码的问题在与:命名不规范,避免使用 String s 之类的,用有实际意义的命名
修改后 第二次代码如下:
public static List<String> union(List<String> period) {
List<String> resultPeriod = new ArrayList<String>();
for(int i = 0;i < period.size();i++){
if (period.get(i) == null) continue;
String[] outerPeriods = period.get(i).split("-");
Arrays.sort(outerPeriods);
for(int j = 0;j < period.size();j++){
if (period.get(j) == null) continue;
String[] innerPeriod = period.get(j).split("-");
Arrays.sort(innerPeriod);
// if (strs1[0].equals(strs2[0]) && strs1[1].equals(strs2[1])) continue;
if (outerPeriods[0].compareTo(innerPeriod[1]) <= 0 && outerPeriods[1].compareTo( innerPeriod[0]) >= 0){
String string = null;
if (outerPeriods[0].compareTo(innerPeriod[0]) < 0){
string = outerPeriods[0] + "-" + innerPeriod[1];
}else{
string =innerPeriod[0] + "-" + outerPeriods[1];
}
period.set(j, null);
period.set(i, string);
}
}
if (period.get(i) != null)
resultPeriod.add(period.get(i));
}
System.out.println(resultPeriod.toString());
return resultPeriod;
}
第二次代码的问题在于: 对时间段进行比较,不要用字符串比较,最好转成时间或者时间戳进行比较,虽然字符串的ascii码比较也是没有问题的,尽量避免for循环里面 try-catch
修改之后 第三次代码如下:
public static List<String> union(List<String> period) {
List<String> resultPeriod = new ArrayList<String>();
for(int i = 0;i < period.size();i++){
if (period.get(i) == null) continue;
Long[] outerDate = getTimestamp(period.get(i).split("-"));
Arrays.sort(outerDate);
for(int j = i;j < period.size();j++){
if (period.get(j) == null) continue;
Long[] innerDate = getTimestamp(period.get(j).split("-"));
Arrays.sort(innerDate);
Long outerStart = outerDate[0];
Long outerEnd = outerDate[1];
Long innerStart = innerDate[0];
Long innerEnd = innerDate[1];
if (outerStart.equals(innerStart) && outerEnd.equals(innerEnd)) continue;
if (outerStart.compareTo(innerEnd) <= 0 && outerEnd.compareTo(innerStart) >= 0){
String mergeRes;
if (outerStart.compareTo(innerStart) < 0){
mergeRes = getDate(outerStart) + "-" + getDate(innerEnd);
}else{
mergeRes =getDate(innerStart) + "-" + getDate(outerEnd);
}
period.set(j, null);
period.set(i, mergeRes);
}
}
if (period.get(i) != null)
resultPeriod.add(period.get(i));
}
System.out.println(resultPeriod.toString());
return resultPeriod;
}
}
public static Long[] getTimestamp(String[] periods){
Long[] date = new Long[2];
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
for(int i = 0 ; i < periods.length ; i++){
date[i] = sdf.parse(periods[i]).getTime();
}
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static String getDate(Long timestamps){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.format(timestamps);
}
第三次代码的问题在于: 使用的两次for循环进行比较,而且会在循环的时候更改数据,最好不要在循环的时候改数据,而且可以通过一次循环实现此需求
修改之后的第四次代码如下:
public static List<String> union(List<String> period) {
Collections.sort(period);
List<String> resultPeriod = new ArrayList<String>();
for(int i = 0;i < period.size();i++){
if(i == period.size() - 1){
resultPeriod.add(period.get(i));
continue;
}
Long[] frontTimes = getTimestamp(period.get(i).split("-"));
Long[] behindTimes = getTimestamp(period.get(i+1).split("-"));
Long frontStart = frontTimes[0];
Long frontEnd = frontTimes[1];
Long behindStart = behindTimes[0];
Long behindEnd = behindTimes[1];
if (frontStart.compareTo(behindEnd) <= 0 && frontEnd.compareTo(behindStart) >= 0){
String mergeRes = getDate(frontStart) + "-" + getDate(behindEnd);
period.set(i+1, mergeRes);
}else{
resultPeriod.add(period.get(i));
}
}
System.out.println(resultPeriod.toString());
return resultPeriod;
}
第四次代码的问题在于: 1.依然有对原集合进行修改的操作,尽量避免;2.工具类一般不处理异常,直接抛出去;3.SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");会耗费很长的时间,最好不要在for循环里面操作,但是也最好不要写成静态的,因为SimpleDateFormat是线程不安全的,static多个线程共享一个某些时候回出现问题,具体参考(http://www.cnblogs.com/zemliu/archive/2013/08/29/3290585.html);4.由于不认真导致简单的代码错误
第五次修改代码如下:
public static List<String> union(List<String> period) throws ParseException{
Collections.sort(period);
List<String> resultPeriod = new ArrayList<String>();
String currentPeriod = period.get(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
for(int i = 0;i < period.size();i++){
Long[] currentTimes = getTimestamp(currentPeriod.split("-"), sdf);
Long[] nextTimes = getTimestamp(period.get(i).split("-"), sdf);
Long currentStart = currentTimes[0];
Long currentEnd = currentTimes[1];
Long behindStart = nextTimes[0];
Long behindEnd = nextTimes[1];
if (currentEnd < behindStart){
resultPeriod.add(currentPeriod);
currentPeriod = period.get(i);
}else {
List<Long> timePoints = new ArrayList<Long>();
timePoints.add(currentStart);
timePoints.add(currentEnd);
timePoints.add(behindStart);
timePoints.add(behindEnd);
Collections.sort(timePoints);
currentPeriod = getDate(timePoints.get(0)) + "-" + getDate(timePoints.get(timePoints.size() - 1));
}
}
resultPeriod.add(currentPeriod);
return resultPeriod;
}
private static Long[] getTimestamp(String[] periods, SimpleDateFormat sdf) throws ParseException{
Long[] date = new Long[periods.length];
for(int i = 0 ; i < periods.length ; i++){
date[i] = sdf.parse(periods[i]).getTime();
}
return date;
}
private static String getDate(Long timestamps){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.format(timestamps);
}
大神说马马虎虎可以提交了
以上是关于今天写工具类时所犯的错误的主要内容,如果未能解决你的问题,请参考以下文章