小编Nam*_*ter的帖子

如何将由空格分隔的整数字符串转换为JAVA中的数组

假设我有一个字符串"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)

但它不起作用,请提供一些帮助.谢谢!

java arrays string

9
推荐指数
2
解决办法
2万
查看次数

静态工厂模式返回类型中的有界通配符

我在 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)

java generics wildcard

5
推荐指数
1
解决办法
542
查看次数

在 Java 中为测试创建 UUID 列表

我想生成一个模拟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)

那么,有没有更好的办法呢?

java testing junit unit-testing mocking

0
推荐指数
1
解决办法
38
查看次数

标签 统计

java ×3

arrays ×1

generics ×1

junit ×1

mocking ×1

string ×1

testing ×1

unit-testing ×1

wildcard ×1