我是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[].或者我可能从根本上对正确的方法感到困惑.
我有类似的字符串
"I am a boy".
Run Code Online (Sandbox Code Playgroud)
我想像这样打印
"I
am
a
boy".
Run Code Online (Sandbox Code Playgroud)
有谁能够帮我?
为什么代码替代(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) 我有一个方法,我想扩展(而不是编写一个基本相同的新方法),通过在参数列表的末尾添加未知数量的参数.
如果我这样做,我是否必须更改对该方法的所有调用?我想问题是,未知参数是否包括根本没有传入参数的情况?
例如,如果我有一个方法:
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包含额外参数的整数数组在哪里.
我想编辑这个方法,而不是编写一个几乎完全相同的新方法,除了它有一些参数添加到查询.我将简单地编写另一种方法,如果通过编辑这个方法,我将不得不改变对该方法的所有其他调用.
我怎样才能File myTempDir = Files.createTempDir(Path path, String prefix, FileAttribute)在windows中使用它.
我正在使用java 7.在linux上,我可以为第3个参数传递PosixFilePermissions.asFileAttributes.
什么是Windows的等效fileAttributes.
我认为这个方法总是需要一个fileAttribute对象.我可以传入一个空的fileAttribute吗?