在Java中如何迭代String对象中的特定元素(ArrayList):

huangapple go评论123阅读模式
英文:

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&lt;String&gt; dnaArray = new ArrayList&lt;&gt;();
ArrayList&lt;Character&gt; compArray = new ArrayList&lt;Character&gt;();

// [tac, tat, tta, aaa, aac, aca, aac, ttt]

public void createCompliment() {
    for (String letters : dnaArray) {
        for (int i = 0, len = letters.length(); i &lt; len; i++) {
            char character = letters.charAt(i);
            if (character == &#39;a&#39;){
                character = &#39;t&#39;;
            } else if (character == &#39;c&#39;) {
                character = &#39;g&#39;;
            }
            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&lt;String&gt; dnaArray = new ArrayList&lt;&gt;();
ArrayList&lt;String&gt; compArray = new ArrayList&lt;String&gt;();

public void createCompliment() {
    for (String letters : dnaArray) {
        compArray.add(letters.replace(&#39;a&#39;, &#39;t&#39;).replace(&#39;c&#39;, &#39;g&#39;));
    }
}

答案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&lt;letters.length(); i++) {
		char character = letters.charAt(i);
        if (character == &#39;a&#39;){
            sb.append(&quot;t&quot;);
        }
        else if (character == &#39;c&#39;){
        	sb.append(&quot;g&quot;);
        } else {
        	sb.append(String.valueOf(character));
        }
	}
	compArray.add(sb.toString());
}

Output:

[ttg, ttt, ttt, ttt, ttg, tgt, ttg, ttt]

huangapple
  • 本文由 发表于 2020年8月14日 17:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63410261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定