本来认为这是编码的基本能力,用来作面试题,
可不知道是大家根本不注意、不需要、还是太难了,咋那么多人答不上来下面代码示例有哪些不规范、低效或错误的地方
public class SampleClass {
  private ArrayList<String> list = null;
  public SampleClass (){
    list = new ArrayList<String>();
    list.add(new String("one"));
    list.add(new String("two"));
    list.add(new String("three"));
    ...
  }
  String SampleMethod(String str)  throws Exception {
    if (str == null && str.equals(""))
      return null;
    
    try {
      ...
      for(String s: list) {
        str += s;
      }
      return str;
    } catch (Exception e) {
      throw new Exception("XXXX");
    }
  }
}

解决方案 »

  1.   

     private ArrayList<String> list = null;换成List面向接口
      for(String s: list) {
            str += s;
          }可能会抛NullPointerException
      

  2.   

      if (str == null && str.equals(""))
    str += s;这2个地方 有
      

  3.   

    1、list.add(new String("one"));
       list.add("one");
    2、if (str == null && str.equals(""))
       if (StringUtils.isNotEmpty(str))
    3、str += s;
       StringBuffer str ,str.append();比+=效率高。
    4、    try {
          ...
          for(String s: list) {
            str += s;
          }
          return str;
        } catch (Exception e) {
          throw new Exception("XXXX");
        }
    throw new Exception("XXXX"); 这里需要return一下。或者把return str 放到try catch 外面。只能看到这些了。
      

  4.   

    new String("one")
    &&
    +=
      

  5.   

    规范:
    1.没注释,函数间没有空行;
    2.catch (Exception e)后别再抛Exception,最好别把异常栈丢掉(不好跟踪);
    3.最好保持一个函数,只有一个return。(前两天csdn有篇译文是这么建议的)低效:
    1.字符串直接"XX"就好,别new String("XX")了;
    2.str == null && str.equals(""),可能会抛nullPointerException,推荐使用"".equals(str)这种方式;(再联系上下文看看,貌似这里的“||”应该是“&&”吧?)--》
    只发现这么些
      

  6.   

    1、没有注释
    2、 private ArrayList<String> list = null;
        换成 private List<String> list = null;更好
    3、new String("one") 换成 "one" 即可。没有必要去new 一下吧。
    4、str == null && str.equals("") 没有搞清楚要干什么?写错了吧。
    5、  return str; 最好放try catch外面。或在外面放个return null;
    不然这个异常扑捉有问题
      

  7.   

    一行行的看:
    1、ArrayList用List来声明;
    2、new ArrayList<String>();改用LinkedList
    3、list.add(new String("one"));改为list.add("one");
    4、SampleMethod方法名首字母小写;
    5、if (str == null && str.equals(""))改为if (str == null || "".equals(str.trim()));
    6、str += s;改为使用StringBuilder,new StringBuilder时放在循环外;
    7、无需tye{}catch(){}结构,已经throws了;
      

  8.   

    楼主为什么觉得没人回答的出来,这些问题很初级,我冒昧回答下。private ArrayList<String> list = null;
    改为 List<String>list
    原因:面向接口编程list.add(new String("one"));
    改为 list.add("one");
    原因:String inner pool问题,参照jdk doc。if (str == null && str.equals(""))
        return null;
    str.equals("")改为"".equals(str)防止空指针异常
    修改后条件仍然不可能成立,直接返回null算了 :>for(String s: list) {
            str += s;
    }
    使用StringBuild进行append,String是final value类,StringBuild会比StringBuffer高效。try {
          ..    
          return str;
    } catch (Exception e) {
          throw new Exception("XXXX");
    }原生异常信息丢失,编码十分忌讳的问题
      

  9.   

    只看出来for循环里面的,字符串连接应该用StringBuilder的append()方法,效率高
      

  10.   

    public class SampleClass {
      private List<String> list = null;// 基本都是这么干的 不会直接用ArrayList定义变量的
      public SampleClass (){
        list = new ArrayList<String>();
        list.add("one"));// new String()的做法好像不太好吧?
        list.add("two");
        list.add("three");
        ...
      }
      String SampleMethod(String str)  throws Exception {
        if (str==null || "".equals(str))//str == null && str.equals("")
          return null;
        StringBuffer strbuf = new StringBuffer();
        try {
          ...
          for(String s: list) {
            strbuf.append(s); 
          }
          return strbuf.toString();
        } catch (Exception e) {
          throw new Exception("XXXX");
        }
      }
    }    让我写就写成这样了  不知道达到标准了没
      

  11.   

    catch (Exception e) {
      throw new Exception("XXXX");
     return null; 
      }
    差点忘记
      

  12.   

    补充一点
     for(String s: list) {
            
     }
    这种方式遍历  效率低。。for(int i=0;i<list.size();i++){}
    推荐用 索引方式遍历 
      

  13.   

    public class SampleClass {
      private ArrayList<String> list = null;  // ==> private List<String> list;
      public SampleClass (){
        list = new ArrayList<String>();
        list.add(new String("one"));   // ==> list.add("one");
        list.add(new String("two"));
        list.add(new String("three"));
      }
      
      String SampleMethod(String str)  throws Exception {  // no checked exception thrown in the code
        if (str == null && str.equals(""))    // && should be ||  ==> if (str == null || str.isEmpty())
          return null;                        // when input str is empty, return null is not logical
                                              // also good practise is not to return null to client                  
        
        try {
          for(String s: list) {                  
            str += s;                      // ==> if performacne is critical or list is huge, use StringBuilder
          }
          return str;
        } catch (Exception e) {            // remove the exception handling block ; no exceptional case in above code
          throw new Exception("XXXX");     // even caught, should not donothing but rethrow it
        }
      }
    }  
      

  14.   


    public class SampleClass {
      private ArrayList<String> list = null;
      public SampleClass (){
        list = new ArrayList<String>();
        list.add(new String("one"));//低效 多生成一个string对象
        list.add(new String("two"));
        list.add(new String("three"));
        ...
      }
      String SampleMethod(String str)  throws Exception {//这种权限控制很少用啊 是不是本意呢
        if (str == null && str.equals(""))//低效 判断length更好些
          return null;
        
        try {
          ...
          for(String s: list) {
            str += s;//低效 应使用stringbuffer 或stringbuilder
          }
          return str;
        } catch (Exception e) { //会捕获包括runtimeexception的所有异常 本意不是这样的话则为不规范
          throw new Exception("XXXX");//不规范 原异常信息丢失
        }
      }
    }    
      

  15.   

    好吧,我承认我很无聊import java.util.*;
    public class SampleClass {
    private List<String> list = null;
    public SampleClass() {
    list = new ArrayList<String>();
    list.add("one");
    list.add("two");
    ...
    }
    String SampleMethod() {
    StringBuilder sb = new StringBuilder()
    for (String s : list)
    sb.append(s);
    return sb.toString();
    }
    }
      

  16.   

    List<String> list = new ArrayList<String>();
    for(int i = 0; i<10000;i++){
    list.add("dfdf");
    }
    long start1 = System.currentTimeMillis();
    for(String str:list){
    System.out.println(str);
    }
    long end1 = System.currentTimeMillis();


    long start = System.currentTimeMillis();
    for(int i=0;i<list.size();i++){
    System.out.println(list.get(i));
    }
    long end = System.currentTimeMillis();
    System.out.println(end1 - start1);
    System.out.println(end - start);1.请你看清楼主题目意思   (下面代码示例有哪些不规范、低效或错误的地方)
    2.不知道你所谓的效率差不多是什么概念   1万条数据可能也就差那么几十毫秒,既然明知道有遍历更快的方式。。 为什么就一定要选择低效率的方式遍历。 难道foreach方式代码可读性 或者什么地方更优于索引的方式? 
      

  17.   

    1. StringBuilder替换StringBuffer    // 效率 
    2. if (str==null || "".equals(str)) // 规范上  即使if语句里面就一句代码 但是不加括号明显不是很规范  
       return null;
    if (str==null || "".equals(str)){   
       return null;
    }
    3.遍历方式上  上面说过了。
    4.应该把  
    for(String s: list) {
      strbuf.append(s);  
    }这段代码 放到try catch外面。 遍历集合 在循环里面没做其他特殊处理是不会抛出Exception 
    5. catch 里面 正常要先输出异常信息,再抛出自定义异常啊。
    6.SampleMethod方法 访问修饰符未申明不规范。  以上仅仅代表个人观点,欢迎大家评判!!
      

  18.   

    public class SampleClass {
      private ArrayList<String> list = null;
      public SampleClass (){
        list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
        ...
      }
      String SampleMethod(String str)  throws Exception {
        if (null == str  && str.equals(""))
          return null;
        
        try {
          ...
          for(String s: list) {
            str += s;
          }
          return str;
        } catch (Exception e) {
          throw new Exception("XXXX");
        }
      }
    }    
    这样就OK 了
      

  19.   


    public class SampleClass {
      private ArrayList<String> list = null;
      public SampleClass (){
        list = new ArrayList<String>();
        list.add(new String("one"));//直接写list.add("one")好一点
        list.add(new String("two"));
        list.add(new String("three"));
        ...
      }
      String SampleMethod(String str)  throws Exception {
        if (str == null && str.equals(""))//这有问题吧   if (str == null || "".equals.str)
          return null;
        
        try {
          ...                            
          for(String s: list) {          
            str += s;//这一句相当于 str=new String(str + s);用StringBuffer就不用new那么多对象了
                      //应该用StringBuffer.append(s);
          }
          return str;//return stringBuffer.toString();
        } catch (Exception e) {
          throw new Exception("XXXX");
        }
      }
    }    
      

  20.   

    str == null && str.equals(""))这里是错误的,这个条件永远不可能是true。
      

  21.   

    没想到啊,这段小小的代码有这么多的问题,受教了。
    一直使用MyEclipse,借助与它强大的IDE功能,好多东西都忽略了
      

  22.   

    不规范:private List<String> list = null;
    低效率:str.equals("");
    错误:str += s;
      

  23.   


    public class SampleClass {
      //private ArrayList<String> list = null;
      private List<String> list = null;//一般会面向接口编程
      /**考虑到执行效率的问题,一般数据初始化的工作不会放在构造函数内,可以提供其它的方法如getData获得数据
      public SampleClass (){
        list = new ArrayList<String>();
        list.add(new String("one"));
        list.add(new String("two"));
        list.add(new String("three"));
        ...
      }
      */
      public List<String> getData() {
        this.list = new ArrayList<String>();
        //考虑到编码效率,一般不会用new String()这种方法...低层的执行效率就不细究了
        list.add("one");
        list.add("two");
        list.add("three");
        ...
      }
      
      //一般方法名都采用首字母小写的驼峰标识,但不是硬性的规范,所以不改了...
      String SampleMethod(String str)  throws Exception {
        //这个条件不成立吧...
        //if (str == null && str.equals(""))
        if (str == null || str.equals(""))  
          //一般不会采用return null,这里用throw new Exception通知调用的客户程序会比较好
          return null;
        if(this.list == null && this.list.size() == 0) getData();    //有throws了,而且没有特定的finally工作,应该不需要try catch了
        //try {
          ...
          for(String s: list) {
            //效率问题就见仁见智了...
            str += s;
          }
          return str;
        //} catch (Exception e) {
          //throw new Exception("XXXX");
        //}
      }
    }   
      

  24.   

     if (str == null && str.equals(""))//不成立吧?你题目出的都很恶心...这个都错还TMD考别人
          return null;