开发者

decompose a string into array of int

What is the shortest and most efficient way to decompose a string array into an array of int's if the string looks like:

4 1 4 2 3
5 1 4 2 -1 6

The way I have it now is to use 开发者_StackOverflow中文版String split method and iterate the String array into int's.. Is there a better way than this?


That's fine - I'd just call split(" ") and then use Integer.parseInt() on the resulting elements.

If you could potentially have more than one space between the ints, you'll need something like split("\\s+") for it to work properly.

If that's what you're doing, I don't think there's a better way!

EDIT: Perhaps I should qualify the term better - I mean that for all practical purposes, unless you're really hitting performance critical issues, I'd stick with this method since it's clean and easy to understand what's going on!


Using split() consumes more space [and time], as new String objects are created, but it is far more elegant [and simple] then any other way.

unless performance is very critical, I'd stick with this way.


Algorithm 3times faster than the split method! :)

Just for the fun of it I have made an algorithm that is far faster than the split method. Maybe nothing you should use as I would say split is cleaner and cleaner is far more important than speed. Also the Donald Knuth quote of premature optimizations are the root cause of all evil.

Output

1189ms // My algorithm first loop
3305ms // Split algorithm runtime first loop
1173ms // My algorithm second loop
3305ms // Split algorithm second loop

The code

import java.util.ArrayList;

class FarmorsOptimized {

@SuppressWarnings({ "rawtypes", "unchecked" })
private static ArrayList getFarmors(String s) {
    ArrayList intArr = new ArrayList();
    int stopvalue = s.length() ;
    for (int i = 0; i < stopvalue;) {
        int negativ = 0;
        if (s.charAt(i) == '-') {
            negativ = 1;
        }
        intArr.add(Integer.parseInt(s.substring(i, i+1+negativ)));
        i+=2+negativ;
    }
    return intArr;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private static ArrayList getSplits(String s) {
    ArrayList intArr = new ArrayList();
    for(String str : s.split(" ")){
        intArr.add(Integer.parseInt(str));
    }
    return intArr;
}

public static void main(String[] args) {
    String s = "1 2 4 -6 7 1 2 4 -6 7 1 2 4 -6 7 1 2 4 -6 7 1 2 4 -6 7 1 2 4 -6 7 1 2 4 -6 7";
    int times = 1000000;
    //Just to init everything
    for (int i = 0; i < times; i++) {
        getFarmors(s);
        getSplits(s);
    }
    long starttime = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
        getFarmors(s);
    }
    System.out.println(System.currentTimeMillis() - starttime);
    starttime = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
        getSplits(s);
    }
    System.out.println(System.currentTimeMillis() - starttime);
    starttime = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
        getFarmors(s);
    }
    System.out.println(System.currentTimeMillis() - starttime);
    starttime = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
        getSplits(s);
    }
    System.out.println(System.currentTimeMillis() - starttime);
}


Answer to comment discussion. This answer works for all ints. It's significantly faster than the split.

1295ms my
2193ms split
1155ms my
1889ms split

code

import java.util.ArrayList;

class FarmorsOptimized {

public static void main(String[] args) {
    String s = "32324 -324 873249 -8544876 -74093 -3243274 4325 643286 92325 -21376218 -213 2132531 2314 1 2";
    int times = 1000000;
    long starttime = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
        getFarmors(s);
    }
    System.out.println(System.currentTimeMillis() - starttime);
    starttime = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
        getSplits(s);
    }
    System.out.println(System.currentTimeMillis() - starttime);
    starttime = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
        getFarmors(s);
    }
    System.out.println(System.currentTimeMillis() - starttime);
    starttime = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
        getSplits(s);
    }
    System.out.println(System.currentTimeMillis() - starttime);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private static ArrayList getFarmors(String s) {
    ArrayList intArr = new ArrayList();
    int stopvalue = s.length();
    for (int i = 0; i < stopvalue;) {
        int j = 0;
        while (true) {
            if ((i + j) == stopvalue || s.charAt(i + j) == ' ') {
                break;
            }
            j++;
        }
        intArr.add(Integer.parseInt(s.substring(i, i + j)));
        i += j + 1;
    }
    return intArr;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private static ArrayList getSplits(String s) {
    ArrayList intArr = new ArrayList();
    String[] strArr = s.split(" ");
    for(int i = 0; i < strArr.length ; i++){
        intArr.add(Integer.parseInt(strArr[i]));
    }
    return intArr;
}

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜