2015-06-25 96 views
0

请原谅我,我是Java新手。这是我目前的计划的一部分。在这里,我读txt文件,并添加文件的某些行到一个ArrayList(工作,因为它应该):获取ArrayList中每个元素的特定字符的频率<String>

public void actionPerformed(ActionEvent e) { 

    ArrayList<String> organismsString = new ArrayList<String>(); 
    boolean printLines = false; 
    StringBuilder organism = new StringBuilder(); 
    if (e.getSource() == openButton) { 
     returnVal = fileChooser.showOpenDialog(null); 
     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      file = fileChooser.getSelectedFile(); 
      //File[] file = hairpinFileChooser.getSelectedFiles(); 
      //read file 
      try { 
       br = new BufferedReader(new FileReader(
        while ((currentLine = br.readLine()) != null) { 
         if (printLines) { 
          if (currentLine.startsWith(">")) { 
           // We have reached the next organism, so stop printing 
           printLines = false; 
           // Add the current organism to our collection 

           organismsString.add(organism.toString()); 


           // Clear the StringBuilder, ready for the next organism 
           organism.setLength(0); 

          } else { 
           // We are still printing the current organism 
           organism.append(currentLine); 

          } 
         } 

         if (currentLine.startsWith(organismId)) { 
    // Print this line, and start printing all lines after this (we don't want to append the current line) 
          //organism.append(currentLine); 
          printLines = true; 

         } 
        } 
        //Adds the final organism in the .txt file 
        organismsString.add(organism.toString()); 

但是我现在想算字母的频率“G”和“ C“在arrayList的每个元素中。

目前我能够统计ArrayList中存在的所有字母的频率,但不是针对特定字母而是针对每个单独元素。该代码我要做到这一点如下:

char [] c = organism.toString().toCharArray(); 
          int sz = c.length; 
          int i = 0, j = 0, counter = 0; 

          for (i = 0; i < sz; i++) { 
           counter = 0; 
           for(j=0; j<sz; j++) { 
            if(j<i && c[i] == c[j]) { 
             break; 
            } 
            if (c[j] == c[i]) { 
             counter++; 
            } 
            if(j == sz-1) { 
            System.out.println("character " + c[i]+ " is present" +counter+" times"); 
            } 
           } 

          } 

如果任何人有我怎么可能能够进而达到这一点,将不胜感激任何帮助或建议!

希望这一切都是有道理的,但如果没有请只问任何问题!

非常感谢:)

+0

你只是想只计算大写字母 “G” 和/或 “C”?或者你还想包括小写字母吗? – Shar1er80

+0

不只是大写的。我希望所有的资本G和C的总频率对于数组列表的每个元素都一致,但是难以实现这一点,尽管我知道这可能是一个简单的解决方案:) – Matt

回答

0

你可以有两个int变量,一个是铯的量,一个用于GS的量。然后,依次循环访问char数组中的元素。如果当前元素等于C,则增加C计数器。如果它等于G,则增加G计数器。

如果你只是想G和C的总数量,然后有一个计数器和增量,每次你遇到一个G或C.

0

我看到这两个潜在的方法。

  1. 循环遍历字符串中的每个字符,并在遇到'C'或'G'时递增计数器。您不必将字符串转换为char []来遍历字符,只需使用String.charAt()即可。
  2. 使用“[^ CG]”的正则表达式执行临时String.replaceAll(),这意味着您要用空字符串替换所有不是'C'或'G'的字符。这将产生一个字符串,只有C和G,您可以拨打String.length()

示例代码:

public static void main(String[] args) throws Exception { 
    String data = "GGGGGCABCKDJ930495860CCCGCGCGCCCGG"; 

    // Loop counting 
    int cgCount = 0; 
    for (int i = 0; i < data.length(); i++) { 
     if (data.charAt(i) == 'C' || data.charAt(i) == 'G') { 
      cgCount++; 
     } 
    } 

    System.out.printf("CG Count: %d\r\n", cgCount); 
    // String.replaceAll with regex pattern 
    System.out.printf("CG Count: %d\r\n", data.replaceAll("[^CG]", "").length()); 
} 

结果:

CG Count: 20 
CG Count: 20