相关疑难解决方法(0)

如何在Java中将int []转换为Integer []?

我是Java的新手,非常困惑.

我有一个长度为4的大型数据集,int[]我想计算4个整数的每个特定组合出现的次数.这与计算文档中的单词频率非常相似.

我想创建一个Map<int[], double>将每个int []映射到一个运行计数的列表迭代,但Map不采用原始类型.

所以我做了 Map<Integer[], Double>

我的数据存储为一个ArrayList<int[]>所以我的循环应该是这样的

ArrayList<int[]> data = ... // load a dataset`

Map<Integer[], Double> frequencies = new HashMap<Integer[], Double>();

for(int[] q : data) {

    // **DO SOMETHING TO convert q from int[] to Integer[] so I can put it in the map

    if(frequencies.containsKey(q)) {
    frequencies.put(q, tfs.get(q) + p);
    } else {
        frequencies.put(q, p);
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定在评论中需要哪些代码才能使这项工作转换int[]Integer[].或者我可能从根本上对正确的方法感到困惑.

java arrays generics collections

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

Java String新行

我有类似的字符串

"I am a boy".
Run Code Online (Sandbox Code Playgroud)

我想像这样打印

"I 
am 
a
boy".
Run Code Online (Sandbox Code Playgroud)

有谁能够帮我?

java string newline

124
推荐指数
11
解决办法
84万
查看次数

Java泛型SuppressWarnings("未选中")之谜

为什么代码替代(1)编译时没有警告,代码替代(2)产生"未经检查的强制转换"警告?

两者共同:

class Foo<T> {
    Foo( T [] arg ) {
    }
}
Run Code Online (Sandbox Code Playgroud)

替代方案(1):

class Bar<T> extends Foo<T> {
    protected static final Object [] EMPTY_ARRAY = {};

    @SuppressWarnings("unchecked")
    Bar() {
         super( (T []) EMPTY_ARRAY );
    }
}
Run Code Online (Sandbox Code Playgroud)

替代方案(2):

class Bar<T> extends Foo<T> {
    @SuppressWarnings("unchecked")
    Bar() {
         super( (T []) EMPTY_ARRAY );
    }

    protected static final Object [] EMPTY_ARRAY = {};
}
Run Code Online (Sandbox Code Playgroud)

备选方案(2)产生:

javac -Xlint:unchecked Foo.java Bar.java 
Bar.java:4: warning: [unchecked] unchecked cast
             super( (T []) EMPTY_ARRAY );
                           ^
  required: T[]
  found: …
Run Code Online (Sandbox Code Playgroud)

java generics unchecked suppress-warnings java-7

11
推荐指数
1
解决办法
2421
查看次数

在Java中向方法调用添加未知数量的参数

我有一个方法,我想扩展(而不是编写一个基本相同的新方法),通过在参数列表的末尾添加未知数量的参数.

如果我这样做,我是否必须更改对该方法的所有调用?我想问题是,未知参数是否包括根本没有传入参数的情况?

例如,如果我有一个方法:

queryFactory(int [] typeArgs, int queryType, int[] ... args){}
Run Code Online (Sandbox Code Playgroud)

我可以打电话:

queryFactory(typeArgsInstce, queryTypeInstce)
Run Code Online (Sandbox Code Playgroud)

然后,当我需要向查询调用添加参数时:

queryFactory(typeArgsInstce, queryTypeInstce, argsInstce)
Run Code Online (Sandbox Code Playgroud)

argsInstce包含额外参数的整数数组在哪里.

我想编辑这个方法,而不是编写一个几乎完全相同的新方法,除了它有一些参数添加到查询.我将简单地编写另一种方法,如果通过编辑这个方法,我将不得不改变对该方法的所有其他调用.

java argument-passing

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

Java 7 nio FileAttributes窗口

我怎样才能File myTempDir = Files.createTempDir(Path path, String prefix, FileAttribute)在windows中使用它.

我正在使用java 7.在linux上,我可以为第3个参数传递PosixFilePermissions.asFileAttributes.

什么是Windows的等效fileAttributes.

我认为这个方法总是需要一个fileAttribute对象.我可以传入一个空的fileAttribute吗?

java windows nio

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