Domanda

I want to find multiple hits with the indexOf method.

I have got this line to search in:

public static final String DNA = "ATGTTGCTGGCATAGATGTTAACTTCCAC";

For example i would like to search the locations of AC in this String. I want to do that with this method. At the moment i only get 21 a couple of times. As i should get 21 and 27 ones.

public int[] zoek(String gezocht)
{
    // goed = 21+27 bij AC
    int fromIndex = 0;
    for (int i = 0; i < DNA.length(); i++)
    {
       fromIndex = DNA.indexOf(gezocht, fromIndex);
       dna.add(fromIndex);

    }
    System.out.println(dna);
    return zoek;
}
È stato utile?

Soluzione

Try this code.

import java.util.ArrayList;

public class Test001
{
    public static final String DNA = "ATGTTGCTGGCATAGATGTTAACTTCCAC";

    public static Integer[] zoek(String gezocht)
    {
        ArrayList<Integer> dna = new ArrayList<Integer>();
        int i = 0;
        while (i < DNA.length())
        {
           int fromIndex = DNA.indexOf(gezocht, i);
           if (fromIndex >= 0){
               dna.add(fromIndex);
               i = fromIndex + 1;
           }else{
               break;
           }
        }
        System.out.println(dna);
        Integer[] zoek = new Integer[dna.size()]; 
        return dna.toArray(zoek);
    }

    public static void main(String args[]) {
        Integer[] ind = zoek("AC");
        for (int fromIndex : ind){
            System.out.println(fromIndex);
        }
    }
}

I would name the variables differently btw.

import java.util.ArrayList;

public class Test001
{
    public static final String DNA = "ATGTTGCTGGCATAGATGTTAACTTCCAC";

    public static Integer[] search(String sought)
    {
        ArrayList<Integer> hits = new ArrayList<Integer>();
        int startIndex = 0;
        while (startIndex < DNA.length())
        {
           int foundIndex = DNA.indexOf(sought, startIndex);
           if (foundIndex >= 0){
               hits.add(foundIndex);
               startIndex = foundIndex + 1;
           }else{
               break;
           }
        }
        System.out.println(hits);
        Integer[] found = new Integer[hits.size()]; 
        return hits.toArray(found);
    }

    public static void main(String args[]) {
        Integer[] found = search("AC");
        for (int foundIndex : found){
            System.out.println(foundIndex);
        }
    }
}

Altri suggerimenti

Use a while-loop instead, something like:

int fromIndex = 0;
while((fromIndex = dna.indexOf(gezocht, fromIndex)) != -1) {
    ...
    fromIndex++; // We don't want to find the previous match again
}

You have to add one to fromIndex so that indexOf won't just find the same occurrence again. You should also not loop as many times as the string has characters, but as long as occurrences are found:

int fromIndex = -1;
while ((fromIndex = DNA.indexOf(gezocht, fromIndex + 1)) != -1) {
   dna.add(fromIndex);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top