From arrayList to long[]

这一生的挚爱 提交于 2020-01-03 11:04:04

问题


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

public long[] parseString(String input)

input are strings like:

  • 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.

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.

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.

Which do you think it's the best?

Is there a better way to do this?

Remember I can't change the method signature :(


回答1:


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.

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.




回答2:


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

return Longs.toArray(foundLongs);

ta-daa!




回答3:


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



回答4:


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



回答5:


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.




回答6:


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



回答7:


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


来源:https://stackoverflow.com/questions/1696791/from-arraylist-to-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!