Java 从 arrayList 到 long[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1696791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:40:58  来源:igfitidea点击:

From arrayList to long[]

javaarrayscollections

提问by Macarse

I am doing a method that needs to return a long[]. It looks like this:

我正在做一个需要返回 long[] 的方法。它看起来像这样:

public long[] parseString(String input)

input are strings like:

输入是字符串,如:

  • 1, 3, 4
  • 10, 30, 40, 50
  • 1、3、4
  • 10、30、40、50

Inside parseString I use a regex to get all numbers and add them to an ArrayList as I can't know how many oconcurrences it will find.

在 parseString 中,我使用正则表达式来获取所有数字并将它们添加到 ArrayList 中,因为我不知道它会找到多少个 oconcurrences。

At the end I create a long[] with the size of the arrayList and do a for each to add it to the long[] var.

最后,我创建了一个 long[] 与 arrayList 的大小,并为每个做一个将它添加到 long[] var。

Another way would be: First count every occurrence with a

另一种方法是:首先用

while ( matcher.find() ) size++;

and then with size create a long[] of size size and do a: matcher.reset() and now save the long values in the long[] variable.

然后使用 size 创建一个大小为 size 的 long[] 并执行:matcher.reset(),现在将 long 值保存在 long[] 变量中。

Which do you think it's the best?

你认为哪个最好?

Is there a better way to do this?

有一个更好的方法吗?

Remember I can't change the method signature :(

请记住我无法更改方法签名:(

采纳答案by Pete Kirkham

Because of the dichotomy between primitives and objects in Java, you can't use the generic list List<Long>.toArray(Long[])to build a primitive array as the result. There are primitive collections which can be used, but either way - using a list or working over the groups - you're copying data from a temporary storage to a primitive array.

由于 Java 中基元和对象之间的二分法,您不能使用泛型列表List<Long>.toArray(Long[])来构建基元数组作为结果。有可以使用的原始集合,但无论哪种方式 - 使用列表或处理组 - 您都将数据从临时存储复制到原始数组。

So either of the ways you suggest is about as good as each other. If it's performance sensitive, profile both and choose the best for your regex.

因此,您建议的任何一种方式都与彼此一样好。如果它对性能敏感,请分析两者并为您的正则表达式选择最好的。

回答by monksy

You can't use toArray in this case. ArrayLists store only objects, and not primitives. Thus, you can not store int, longs, etc. You will have to either store all the objects as Long objects or create a static array of longs yourself.

在这种情况下不能使用 toArray。ArrayLists 只存储对象,而不存储基元。因此,您不能存储 int、longs 等。您必须将所有对象存储为 Long 对象或自己创建一个静态的 long 数组。

回答by KM?n

I think you can also do something like.

我认为你也可以做类似的事情。

public long[] parseString(String input)
{
            //1. Split with comma separated
            int nLength = input.Split(new char[] { ',' }).Length;
            long[] arList = new long[nLength];

            for (int i = 0; i < nLength; i++)
            {
                arList[i] = long.Parse(input.Split(new char[] { ',' })[i].ToString());
            }

            return arList;
}

Usage:

用法:

long[] l = parseString("10, 30, 40, 50");

回答by mhaller

@Test
public void testParsing() throws Exception {
    String input = "1,3,5,6,33";
    long[] parsed = parseString(input);
    assertEquals(5, parsed.length);
    assertEquals(1, parsed[0]);
    assertEquals(3, parsed[1]);
    assertEquals(5, parsed[2]);
    assertEquals(6, parsed[3]);
    assertEquals(33, parsed[4]);
}
public long[] parseString(String input) {
    String[] split = input.split(Pattern.quote(","));
    long[] arr = new long[split.length];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = Long.parseLong(split[i]);
    }
    return arr;
}

回答by setzamora

public long[] parseString(String input) {

        final String[] parsed = input.split(",\s?");
        final long[] values = new long[parsed.length];

        for (int i = 0; i < parsed.length; i++) {
            values[i] = Long.parseLong(parsed[i]);
        }

        return values;
}

回答by Cowan

At the risk of being a huge pimp for Google's guava-libraries(I really do love it!), the Longsclass makes this a one-liner:

冒着成为谷歌番石榴库巨大皮条客的风险(我真的很喜欢它!),Longs类使它成为一个单行:

return Longs.toArray(foundLongs);

ta-daa!

哒哒!

回答by Sonu

List<Long> lErrors = new ArrayList<Long>();
lErrors.add(10L);
Long[] arr = null;
arr = new Long[lErrors.size()];
lErrors.toArray(arr);