Java – what is the best way to count and sort string arrays?

I try to find out whether there is a good search method (counting the number of occurrences), and then sort a string array in an effective way... This is a way that works well in embedded system (32MB),

Example: I must calculate the time used for characters a, B, C, etc. and save the result of this consequence classification

I can count using the public int count (string searchdomain, char SearchValue) method, but each string should have all alphabets, for example:

"This is a test string"
A:1,B:0,C:0,D:0,E:1,I:3,F:0,...
"ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCC"
A:7,C:22,G:18

My sorting method needs to be able to answer the following questions: sort by the number of as and B, sort by as first, and then sort by BS

This is not a job. It is an application that needs to run on the mobile phone. I need it to be efficient. My current implementation is too slow and uses too much memory

Solution

I'll take advantage of Java's (very efficient) built - in sorting capabilities First, define a simple class to contain your string and its metadata:

class Item
{
    // Your string. It's public,so you can get it if you want,// but also final,so you can't accidentally change it.
    public final String string;

    // An array of counts,where the offset is the alphabetical position
    // of the letter it's counting. (A = 0,B = 1,C=2...)
    private final short[] instanceCounts = new short[32];

    public Item(String string)
    {
        this.string = string;
        for(char c : string.tocharArray())
        {
            // Increment the count for this character
            instanceCounts[(byte)c - 65] ++;
        }
    }

    public int getCount(char c)
    {
        return instanceCounts[(byte)c - 65];
    }
}

This will keep your string (for search and display) and set a short array of matching characters If your memory is really low, you know that any character of your string has more than 255 characters. You can even change it into a byte array A short has only 16 bytes, so the array itself can only take 64. No matter how complex the string is, all bytes are together If you are willing to calculate the performance value of each count, you can get rid of the array and replace the getcount () method, but you may eventually save one-time memory by consuming frequent garbage collection, which is a great performance

The above is the Java compiled by programming house for you - what is the best way to count and sort string arrays? I hope this article can help you solve Java - what is the best way to count and sort string arrays? Program development problems encountered.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>