简体   繁体   English

将数组列表中的单个值存储到数组中

[英]storing single values from an arraylist into an array

I want to get values from an arraylist so that I can graph them but I am having trouble getting them into an array. 我想从数组列表中获取值,以便可以绘制它们的图形,但是我很难将它们放入数组中。 In this loop, I am looping over my sorted hashmap and attempting to store values in an array(doesn't work). 在此循环中,我遍历排序的哈希图,并尝试将值存储在数组中(不起作用)。

long v [] = null;

for(Map.Entry<String,NumberHolder> entry : entries)
{
       v = entry.getValue().singleValues;
}

This is my NumberHolder class 这是我的NumberHolder课

private static class NumberHolder
{
    public int occurrences = 0;
    public int sumtime_in_milliseconds = 0; 
    public ArrayList<Long> singleValues = new ArrayList<Long>();
}

I eventually want an array filled with single values so that I can graph it. 我最终希望用单个值填充数组,以便可以对其进行绘图。

EDIT 编辑

This is what I have now, and Eclipse gives me this never ending error suggestions of converting my array to Object[] which gives me problems when using dataset.addSeries for my graph. 这就是我现在所拥有的,并且Eclipse为我提供了这个永无休止的错误建议,即将数组转换为Object[] ,这给我在为图表使用dataset.addSeries时出现了问题。

for(Map.Entry<String,NumberHolder> entry : entries)
{ 
   graphMaker(entry.getValue().occurrences,entry.getValue().singleValues.toArray());
}                                        


public static void graphMaker(int totalValues, Object[] objects)
{

    int bin = 1000;
    HistogramDataset dataset = new HistogramDataset();
    dataset.setType(HistogramType.RELATIVE_FREQUENCY);
    dataset.addSeries("Histogram",objects,bin, 0, 120000);

You can't assign an ArrayList to an Array like that. 您不能像这样将ArrayList分配给Array。 Maybe you could use the toArray method: 也许您可以使用toArray方法:

long v [] = null;

for(Map.Entry<String,NumberHolder> entry : entries)
{
   v = entry.getValue().singleValues.toArray(new long[0]);
}

Pretty straightforward: 很简单:

final ArrayList<Long> t = entry.getValue().singleValues;
v = new long[t.size()];
for (int i = 0; i < v.length; ++i) v[i] = t.get[i];

Edit: fixed handling of primitive type 编辑:固定处理原始类型

v = entry.getValue().singleValues.toArray(new long[0]);

You can't do it directly because long is not equal to Long . 您不能直接这样做,因为long不等于Long

You can use the Apache Commons Lang Library . 您可以使用Apache Commons Lang库 There is a class called ArrayUtils that have a function called toPrimitive . 有一个名为ArrayUtils的类,具有一个名为toPrimitive的函数。

If you use that function you can do: 如果使用该功能,则可以执行以下操作:

for(Map.Entry<String,NumberHolder> entry : entries)
{
   v = ArrayUtils.toPrimitive(entry.getValue().singleValues.toArray(new Long[0]));
}

Or, if you don't want to use the library, you can do the following: 或者,如果您不想使用该库,则可以执行以下操作:

for(Map.Entry<String,NumberHolder> entry : entries)
{
   v = new long[entry.getValue().singleValues.size()];
   for (int i=0; i<v.length; i++) {
       v[i]=entry.getValue().singleValues.get(i);
   }
}

Try the following: 请尝试以下操作:

Set<Long> values = new HashSet<Long>();
Iterator<NumberHolder> it = entries.keySet().iterator();
while(it.hasNext()) {
    NumberHolder numberHolder = it.next();
    values.addAll(numberHolder.singleValues;
}
Long[] v1 = values.toArray();
// transform to primitive long array
long[] v = new long[v1.size()];
for(int i = 0; i < v1.size(); i++) {
    v[i] = v1[i];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM