英文:
how to iterate over specific elements in string objects (arraylist Java)
问题
ArrayList<String> dnaArray = new ArrayList<>();
ArrayList<Character> compArray = new ArrayList<>();
// [tac, tat, tta, aaa, aac, aca, aac, ttt]
public void createCompliment() {
for (String letters : dnaArray) {
StringBuilder complement = new StringBuilder(); // Create a StringBuilder to store the complemented string
for (int i = 0, len = letters.length(); i < len; i++) {
char character = letters.charAt(i);
if (character == 'a') {
complement.append('t'); // Append 't' for 'a'
} else if (character == 'c') {
complement.append('g'); // Append 'g' for 'c'
} else {
complement.append(character); // Keep other characters unchanged
}
}
compArray.add(complement.toString()); // Add the complemented string to the compArray
}
}
这是您的代码的修改部分,用于创建互补字符串并将其添加到compArray
中。最后,您将得到您所期望的输出:
[ttg, ttt, ttt, ttt, ttg, tgt, ttg, ttt]
英文:
ArrayList<String> dnaArray = new ArrayList<>();
ArrayList<Character> compArray = new ArrayList<Character>();
// [tac, tat, tta, aaa, aac, aca, aac, ttt]
public void createCompliment() {
for (String letters : dnaArray) {
for (int i = 0, len = letters.length(); i < len; i++) {
char character = letters.charAt(i);
if (character == 'a'){
character = 't';
} else if (character == 'c') {
character = 'g';
}
compArray.add(character);
}
}
}
How would I access specific characters, change 'a' to 't' and similarily from 'c' to 'g'.
Then added to a new arraylist and return something like:
[ttg, ttt, ttt, ttt, ttg, tgt, ttg, ttt]
instead of: [t, t, g, t, t, t, t, t, t, t, t, t, t, t, g, t, g, t, t, t, g, t, t, t]
appreciate it
答案1
得分: 1
在我看来,最简单的方法是使用字符串替换:
ArrayList<String> dnaArray = new ArrayList<>();
ArrayList<String> compArray = new ArrayList<>();
public void createCompliment() {
for (String letters : dnaArray) {
compArray.add(letters.replace('a', 't').replace('c', 'g'));
}
}
英文:
the easiest way in my opinion is to use string replace:
ArrayList<String> dnaArray = new ArrayList<>();
ArrayList<String> compArray = new ArrayList<String>();
public void createCompliment() {
for (String letters : dnaArray) {
compArray.add(letters.replace('a', 't').replace('c', 'g'));
}
}
答案2
得分: 1
你可以使用StringBuilder()
来创建新的字符串,然后将其添加到compArray
中。
尝试这样做:
for(String letters : dnaArray) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<letters.length(); i++) {
char character = letters.charAt(i);
if (character == 'a'){
sb.append('t');
}
else if (character == 'c'){
sb.append('g');
} else {
sb.append(String.valueOf(character));
}
}
compArray.add(sb.toString());
}
输出:
[ttg, ttt, ttt, ttt, ttg, tgt, ttg, ttt]
英文:
You can use a StringBuilder()
to create the new String then add it to compArray
.
Try this:
for(String letters : dnaArray) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<letters.length(); i++) {
char character = letters.charAt(i);
if (character == 'a'){
sb.append("t");
}
else if (character == 'c'){
sb.append("g");
} else {
sb.append(String.valueOf(character));
}
}
compArray.add(sb.toString());
}
Output:
[ttg, ttt, ttt, ttt, ttg, tgt, ttg, ttt]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论