请原谅我,我是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");
}
}
}
如果任何人有我怎么可能能够进而达到这一点,将不胜感激任何帮助或建议!
希望这一切都是有道理的,但如果没有请只问任何问题!
非常感谢:)
你只是想只计算大写字母 “G” 和/或 “C”?或者你还想包括小写字母吗? – Shar1er80
不只是大写的。我希望所有的资本G和C的总频率对于数组列表的每个元素都一致,但是难以实现这一点,尽管我知道这可能是一个简单的解决方案:) – Matt