开发者

From arrayList to long[]

开发者 https://www.devze.com 2022-12-10 08:34 出处:网络
I am doing a method that needs to return a long[]. It looks l开发者_高级运维ike this: public long[] parseString(String input)

I am doing a method that needs to return a long[]. It looks l开发者_高级运维ike 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 :(


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!


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.


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


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


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消