我需要使用ON循环替换数组中的字符。我不能做比这更先进的事情。我该怎么办?

时间:2018-04-25 19:44:20

标签: java arrays loops character

我正在使用人造DNA链分析仪,我需要为此创建补充链:

char [] testStrand = {'A', 'T', 'T', 'A', 'G', 'C', 'T', 'A', 'T', 'G', 'A', 'A', 'C', 'C', 'T', 'A', 'C', 'C', 'A', 'T'};

注意:这些是20个字符,如果有帮助的话。

我无法对此进行硬编码,我需要使用一个循环来遍历每个角色并找出如何将A与T&#39进行交换(反之亦然)和G&# #39; s与C'(反之亦然。)

3 个答案:

答案 0 :(得分:0)

最简单的解决方案:

char[] testStrand = {'A', 'T', 'T', 'A', 'G', 'C', 'T', 'A', 'T', 'G', 'A', 'A', 'C', 'C', 'T', 'A', 'C', 'C', 'A', 'T'};
char[] newTestStrand = new char[testStrand.length];
for (int i = 0, l = testStrand.length; i < l; i++)
{
   if (testStrand[i] == 'A')
   {
      newTestStrand[i] = 'T';
   }
   else if (testStrand[i] == 'T')
   {
      newTestStrand[i] = 'A';
   }
   else if (testStrand[i] == 'C')
   {
      newTestStrand[i] = 'G';
   }
   else if (testStrand[i] == 'G')
   {
      newTestStrand[i] = 'C';
   }
   else
   {
      newTestStrand[i] = testStrand[i];
   }
}

或使用switch语句(不确定是否允许使用它):

char[] testStrand = {'A', 'T', 'T', 'A', 'G', 'C', 'T', 'A', 'T', 'G', 'A', 'A', 'C', 'C', 'T', 'A', 'C', 'C', 'A', 'T'};
char[] newTestStrand = new char[testStrand.length];
for (int i = 0, l = testStrand.length; i < l; i++)
{
   switch (testStrand[i])
   {
      case 'A':
        newTestStrand[i] = 'T';
        break;
      case 'T':
        newTestStrand[i] = 'A';
        break;
      case 'G':
        newTestStrand[i] = 'C';
        break;
      case 'C':
        newTestStrand[i] = 'G';
        break;
      default:
        newTestStrand[i] = testStrand[i];
        break;
    }
}

但实际上我建议以某种方式表示掉期(键值对)并循环通过潜在的掉期。我坚持使用简单的解决方案,因为你说'#34;仅循环&#34;

答案 1 :(得分:0)

由于有人已经建议使用else / if方法,所以这里使用的是三元运算符:

char[] testStrand = {'A', 'T', 'T', 'A', 'G', 'C', 'T', 'A', 'T', 'G', 'A', 'A', 'C', 'C', 'T', 'A', 'C', 'C', 'A', 'T'};
char[] reversed = new char[testStrand.length];

for (int i = 0; i < testStrand.length; i++) {
    if (testStrand[i] == 'A') {
        reversed[i] = 'T';
    } else if (testStrand[i] == 'T') {
        reversed[i] = 'A';
    } else if (testStrand[i] == 'C') {
        reversed[i] = 'G';
    } else if (testStrand[i] == 'G') {
        reversed[i] = 'C';
    } else {
        reversed[i] = testStrand[i];
    }
}

更新

我注意到你问过如何在不同的数组中插入新值:

Character[] testStrand = {'A', 'T', 'T', 'A', 'G', 'C', 'T', 'A', 'T', 'G', 'A', 'A', 'C', 'C', 'T', 'A', 'C', 'C', 'A', 'T'};
List<Character> chars = Arrays.asList(testStrand);
chars.replaceAll(c -> {
    switch (c) {
        case 'A': return 'T';
        case 'T': return 'A';
        case 'C': return 'G';
        case 'G': return 'C';
        default: return c;
    }
});

// print them out
chars.forEach(System.out::println);

更新2

但是,嘿,如果你感到疯狂:

hdfs dfs -ls

答案 2 :(得分:-1)

Jimmyv代码: 更好?

char[] complementstrand = {'A', 'T', 'T', 'A', 'G', 'C', 'T', 'A', 'T', 'G', 'A', 'A', 'C', 'C', 'T', 'A', 'C', 'C', 'A', 'T'};
    for (int i = 0; i < complementstrand.length; i++) {

    if (Character.toString(complementstrand[i]).equals("A")){
complementstrand[i] = "T".charAt(0);
}

    else if (Character.toString(complementstrand[i]).equals("T")){
complementstrand[i] = "A".charAt(0);
}


    else if (Character.toString(complementstrand[i]).equals("G")){
complementstrand[i] = "C".charAt(0);
}


    else if (Character.toString(complementstrand[i]).equals("C")){
complementstrand[i] = "G".charAt(0);
}


}
System.out.println(Arrays.toString(complementstrand));
相关问题