如何在Java中将int []转换为Integer []? [英] How to convert int[] to Integer[] in Java?

查看:198
本文介绍了如何在Java中将int []转换为Integer []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手,很困惑。

I'm new to Java and very confused.

我有一个长度为4的大数据集 int [] 并且我想计数出现4个整数的每个特定组合的次数
。这与在文档中计数字频率非常相似。

I have a large dataset of length 4 int[] and I want to count the number of times that each particular combination of 4 integers occurs. This is very similar to counting word frequencies in a document.

我想创建一个 Map 在列表被迭代时将每个int []映射到一个运行计数,但Map不使用原始类型。

I want to create a Map<int[], double> that maps each int[] to a running count as the list is iterated over, but Map doesn't take primitive types.

所以我做了 Map

我的数据存储为 ArrayList< int [ > 所以我的循环应该是像

my data is stored as an ArrayList<int[]> so my loop should be something like

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);
    }
}

我不知道我需要什么代码注释以使此工作将 int [] 转换为 Integer []

I'm not sure what code I need at the comment to make this work to convert an int[] to an Integer[]. Or maybe I'm fundamentally confused about the right way to do this.

推荐答案

如果你想转换一个 int [] Integer [] ,在JDK中没有自动的方法。但是,你可以这样做:

If you want to convert an int[] to an Integer[], there isn't an automated way to do it in the JDK. However, you can do something like this:

int[] oldArray;

... // Here you would assign and fill oldArray

Integer[] newArray = new Integer[oldArray.length];
int i = 0;
for (int value : oldArray) {
    newArray[i++] = Integer.valueOf(value);
}

如果您有权访问 Apache lang 库,那么您可以使用 ArrayUtils.toObject(int []) 方法如下:

If you have access to the Apache lang library, then you can use the ArrayUtils.toObject(int[]) method like this:

Integer[] newArray = ArrayUtils.toObject(oldArray);

这篇关于如何在Java中将int []转换为Integer []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆