模式在循环中查找字符串的麻烦

时间:2014-01-27 16:57:21

标签: java

我的getCount()方法在这种情况下应返回2,但它返回7.我认为它错误计数的原因是因为它循环了7次因为这是字符串的长度。但是,我只是想扫描字符串中的模式,并且每次在我正在扫描的字符串中出现模式时,将patternCount增加1。这是我的代码:

package a2;

public class DNAStrandAdept {

    private String strand;
    private String pattern;
    private String passedStrand;
    private int ACount;
    private int CCount;
    private int GCount;
    private int TCount;
    private int patternCount = 0;

    public static void main(String[] args) {
        DNAStrandAdept test = new DNAStrandAdept("AGGTTGG");
        System.out.println("A count: " + test.getACount());
        System.out.println("C count: " + test.getCCount());
        System.out.println("G count: " + test.getGCount());
        System.out.println("T count: " + test.getTCount());
        System.out.println("Strand: " + test.getStrandString());
        System.out.println("Strand length: " + test.getLength());
        System.out.println("Pattern Count: " + test.getCount("GG"));

    }

    public DNAStrandAdept(String strand) {
        passedStrand = strand;
        if (passedStrand.contains("a") || passedStrand.contains("c")
                || passedStrand.contains("g") || passedStrand.contains("t")) {
            throw new RuntimeException("Illegal DNA strand");
        } else if (passedStrand.contains("1") || passedStrand.contains("2")
                || passedStrand.contains("3") || passedStrand.contains("4")
                || passedStrand.contains("5") || passedStrand.contains("6")
                || passedStrand.contains("7") || passedStrand.contains("8")
                || passedStrand.contains("9") || passedStrand.contains("0")) {
            throw new RuntimeException("Illegal DNA Strand");
        } else if (passedStrand.contains(",") || passedStrand.contains(".")
                || passedStrand.contains("?") || passedStrand.contains("/")
                || passedStrand.contains("<") || passedStrand.contains(">")) {
            throw new RuntimeException("Illegal DNA Strand");
        }
    }

    public int getACount() {
        for (int i = 0; i < passedStrand.length(); i++) {
            if (passedStrand.charAt(i) == 'A') {
                ACount++;
            }

        }
        return ACount;
    }

    public int getCCount() {
        for (int i = 0; i < passedStrand.length(); i++) {
            if (passedStrand.charAt(i) == 'C') {
                CCount++;
            }

        }
        return CCount;
    }

    public int getGCount() {
        for (int i = 0; i < passedStrand.length(); i++) {
            if (passedStrand.charAt(i) == 'G') {
                GCount++;
            }

        }
        return GCount;
    }

    public int getTCount() {
        for (int i = 0; i < passedStrand.length(); i++) {
            if (passedStrand.charAt(i) == 'T') {
                TCount++;
            }
        }
        return TCount;
    }

    public String getStrandString() {
        return passedStrand;
    }

    public int getLength() {
        return passedStrand.length();
    }

    public int getCount(String pattern) {

        for (int i = 0; i < passedStrand.length(); i++) {
            if (passedStrand.contains(pattern)) {
                patternCount++;
            }
        }

        return patternCount;
    }

    public int findPattern(String pattern, int startIndex) {
        return 0;
    }
}

这是我的输出:

A count: 1
C count: 0
G count: 4
T count: 2
Strand: AGGTTGG
Strand length: 7
Pattern Count: 7

1 个答案:

答案 0 :(得分:1)

注意您的for循环:

for (int i = 0; i < passedStrand.length(); i++) {
        if (passedStrand.contains(pattern)) {
            patternCount++;
        }
    }

如果pattern中有passedStrand,则true始终为passedStrand.length()。它并不真正依赖于循环的任何部分。由于循环运行true次,因此将多次检查该条件。每次,由于它是patternCountpatternCount会递增。因此passedStrand.length();的最终值为pattern.length()

您更愿意做的是,从每个索引开始,检查下一个pattern个字符数是否构成等于patternCount的字符串。如果是,则递增substring。因此,您需要在此处使用int patternLen = pattern.length(); for (int i = 0; i < passedStrand.length() - patternLen + 1; i++) { if (passedStrand.substring(i, i + patternLen).equals(pattern)) { patternCount++; } } 方法:

passedStrand

另请注意,循环不会真正运行直到pattern字符串结束。你只需要运行直到索引,从那里可能完全出现String字符串。


由于for调用,此方法在substring循环内创建了额外的String#indexOf个对象。您可以使用index方法来避免这种情况。您只需继续查找patternpassedStrand的下一个index,直到-1int startIndex = passedStrand.indexOf(pattern); while (startIndex != -1) { patternCount++; startIndex = passedStrand.indexOf(pattern, startIndex + pattern.length()); } ,然后结束。{/ p >

public int getCount(String pattern) {
    int patternCount = 0;

    Matcher matcher = Pattern.compile(pattern).matcher(passedStrand);

    while (matcher.find()) {
        patternCount++;
    }
    return patternCount;
}

如果效率不是一个大问题,那么正则表达式真的很甜蜜。了解如何:

{{1}}