假设我有一个字符串"1 23 40 187 298".该字符串仅包含整数和空格.如何将此字符串转换为整数数组,即[1,23,40,187,298].这就是我的尝试方式
public static void main(String[] args) {
String numbers = "12 1 890 65";
String temp = new String();
int[] ary = new int[4];
int j=0;
for (int i=0;i<numbers.length();i++)
{
if (numbers.charAt(i)!=' ')
temp+=numbers.charAt(i);
if (numbers.charAt(i)==' '){
ary[j]=Integer.parseInt(temp);
j++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,请提供一些帮助.谢谢!
我在 Effective Java 中读到你不应该使用有界通配符作为返回类型,但我不知道我应该怎么做。我的代码编译的唯一方法是RequestCloner<? extends HttpUriRequest>
在静态工厂中使用作为返回类型。我做错了什么还是有解决方法?
注意:要注意的一件事是HttpUriRequest
有方法setHeader
,但只有HttpPost
方法setEntity
。
abstract class RequestCloner<T extends HttpUriRequest> {
protected T clonedRequest;
private enum RequestType {
GET, POST, DELETE
}
static RequestCloner<? extends HttpUriRequest> newInstance(
String type, String url) {
RequestType requestType = RequestType.valueOf(type);
switch (requestType) {
case GET:
return new GetRequestCloner(url);
case POST:
return new PostRequestCloner(url);
case DELETE:
return new DeleteRequestCloner(url);
default:
throw new IllegalArgumentException(String.format(
"Method '%s' not supported",
type));
}
}
public abstract HttpUriRequest …
Run Code Online (Sandbox Code Playgroud) 我想生成一个模拟List<UUID>
并使用随机 UUID 填充它。但是,我不确定通过创建列表和添加项目是否有更合适的方法而不是以下方法。
List<UUID> uuidList = new ArrayList<>();
uuidList.add(UUID.randomUUID());
uuidList.add(UUID.randomUUID());
uuidList.add(UUID.randomUUID());
Run Code Online (Sandbox Code Playgroud)
那么,有没有更好的办法呢?