|
|
1
3
这是我的看法——它满足了你问题中的所有要求。你会注意到我选择了拒绝非法分配
package com.pragmaticsoftwaredevelopment.stackoverflow;
public class FixedLengthString {
private String string=null;
private int length=-1;
public FixedLengthString(int length) {
this.length = length;
}
public void setValue (String str) {
if (str.length() > length)
throw new IllegalArgumentException("ERROR: Assigned string length (" + str.length() + ") exceeded given limit (" + length + ")");
else
this.string = str;
}
public String toString() {
return String.format("%1$-" + length + "s", string);
}
public String getString() {
return toString();
}
}
|
|
2
2
|
|
|
3
2
可以使用stringutils,它是commons lang的一部分,并使用以下方法:
这样地:
然后你可以把它写到一个文件中——看看commons-io-fileutils,找到一个简单的方法来编写一个字符串… 第二点……构造函数不能返回值,否则它将是标准方法,但可以根据传递给它的参数自由引发异常-在这种情况下,illegalargumentException是最合适的。 |
|
|
4
0
你可以这样做:
|
|
|
user29759326 · 如何返回递归函数中的最后一个值? 1 年前 |
|
|
malife89 · 将java中的字符串读取为正确的日期格式 1 年前 |
|
|
Tim · 在java中,有没有更快的方法将字节数组写入文件? 1 年前 |
|
|
rudraraj · java中未声明最终变量 1 年前 |
|
|
Bala Ji · 以下BFS的实施效率如何? 1 年前 |