1

只是一个快速的形式问题。在下面的代码中,是否有更好的方法来连接字符串(即我可以将 tmpError 设置为等于新字符串而不是添加它吗?)

public void validate () throws Exception {
    String tmpError = "";
    if(paramA == null) tmpError = tmpError + "paramA was not set";

    if(paramB == null) tmpError = tmpError + "paramB was not set";

    if(paramC == null) tmpError = tmpError + "paramC was not set";


    if(!tmpError.equalsIgnoreCase("")){
        tmpError = "error occured" + tmpError;
        throw new Exception(tmpError);
    }
}

提前致谢

4

8 回答 8

4

我总是建议使用 StringBuilder

像这样的东西:

public void validate() throws Exception {
    StringBuilder error = new StringBuilder();
    if(paramA == null)
        error.append("paramA was not set");

    if(paramB == null)
        error.append("paramB was not set");

    if(paramC == null)
        error.append("paramC was not set");


    if(error.length() > 0) {

        throw new Exception("error occured " + error.toString());
    }
}
于 2013-03-21T10:47:22.887 回答
4

使用 . 可以很容易地提高效率StringBuilder

由于 的不可变特性String,每次使用加法 ( +) 运算符进行字符串连接时,String都会分配一个新对象(使用 时也是如此String.concat())。

StringBuilder保留一个内部字符数组,以便在该数组上进行连接操作,并且在调用其方法String时只分配一个对象。toString()使用它的append()方法将文本附加到字符串的末尾,并使用insert()偏移量 0 来附加文本。

但是,您还应该考虑可读性。Dasblinkenlight 在他的回答中提出了一个很好的观点。正如 Anthony 已经指出的那样,您还可以使用+=复合赋值运算符来增强可读性。

public void validate () throws Exception {
    StringBuilder tmpError = new StringBuilder();
    if(paramA == null) tmpError.append("paramA was not set");

    if(paramB == null) tmpError.append("paramB was not set");

    if(paramC == null) tmpError.append("paramC was not set");


    if(tmpError.length() > 0){
        tmpError.insert(0,"error occured");
        throw new Exception(tmpError.getString());
    }
}
于 2013-03-21T10:42:14.693 回答
1

Fastest way to concatenation two strings is concat function of String class.

public void validate () throws Exception {
    String tmpError = "";

    if(paramA == null) tmpError = tmpError.concat("paramA was not set");
    if(paramB == null) tmpError = tmpError.concat("paramB was not set");
    if(paramC == null) tmpError = tmpError.concat("paramC was not set");
    if(!tmpError.equalsIgnoreCase("")){
        tmpError = "error occured".concat(tmpError);
        throw new Exception(tmpError);
    }
}
于 2013-03-21T10:50:48.093 回答
1

您可能想要使用Guava Preconditions而不是上述任何一种。然后你可以写这样的代码:

import static com.google.common.base.Preconditions.*;
...
public void doSomething(String strA, String strB) {
  checkNotNull(strA, "strA is missing");
  checkArgument(strB.length() >= 6, "strB is too short");
  ...
}

如果检查失败,则Exception抛出一个。它可能不像您最初的解决方案那么简洁,但您的意图在语义上是显而易见的。

于 2013-03-21T11:23:46.803 回答
1

您可以使用+=(加法/连接和赋值)运算符:

if(paramA == null) tmpError += "paramA was not set";
于 2013-03-21T10:42:02.023 回答
1

This is not efficient, because with all three params missing you will create four string objects. You would be better off appending to a single StringBuilder object.

However, this is error reporting code which gets executed only when your code detects a programming error. Efficiency does not matter much in situations like that, because they are not supposed to happen in the first place. Use whatever you believe to be easier to understand.

于 2013-03-21T10:43:00.100 回答
1

Do it like this:

public void validate () throws Exception {
    String tmpError = "";
    if(paramA == null) tmpError += "paramA was not set";

    if(paramB == null) tmpError += "paramB was not set";

    if(paramC == null) tmpError += "paramC was not set";


    if(!tmpError.equalsIgnoreCase("")){
        tmpError = "error occured" + tmpError;
        throw new Exception(tmpError);
    }
}

Or alternatively, you can use a StringBuilder as pointed out by Xavi, but to me that would only make sense, if you were appending text inside some sort of loop.

于 2013-03-21T10:43:10.670 回答
0

我会做如下的事情 public void validate () throws Exception {

public void validate () throws Exception {
    String tmpError = "";
    if(paramA == null) tmpError += "paramA was not set";
    if(paramB == null) tmpError += "paramB was not set";
    if(paramC == null) tmpError += "paramC was not set";

    if(!(tmpError.trim()).equalsIgnoreCase("")){
        tmpError = "error occured" + tmpError;
        throw new Exception(tmpError);
    }
}

在检查变量是否为空之前,请参阅我已经使用+=并修剪了变量。tmpError

我认为在这里您不必担心太多,StringBuilder因为在这种情况下处理的内存不多。

如果您坚持使用 StringBuilder,那么您可以执行以下操作

public void validate () throws Exception {
    StringBuilder sb = new StringBuilder();
    if(paramA == null) sb.append("paramA was not set");
    if(paramB == null) sb.append("paramB was not set");
    if(paramC == null) sb.append("paramC was not set");

    if(!(sb.toString().trim()).equalsIgnoreCase("")){
        sb.Insert(0,"error occured");
        throw new Exception(sp.toString());
    }
}
于 2013-03-21T10:44:42.783 回答