简体   繁体   English

如何在java中将char值添加到char数组

[英]how to add char values to char array in java

I'm trying to put the complement results from a char into a char array complement1.我试图将一个字符的补码结果放入一个字符数组补码1中。 I thought I can use the operator but that's only for strings but I'm having a hard time trying to figure out how to put each letter into the array so it can display the results.我以为我可以使用运算符,但这仅适用于字符串,但我很难弄清楚如何将每个字母放入数组中,以便它可以显示结果。 Any help would be great thank you.任何帮助都会非常感谢。

    public static char[] complement(char[] dna) {
        
        char[] complement1 = new char[17];
        
        char comp = '\0';
        
        for (char i = 0; i < dna.length; i++)
            if (dna[i] == 'A') {
                comp = 'T';
            } else if (dna[i] == 'T') {
                comp = 'A';
            } else if (dna[i] == 'G') {
                comp = 'C';
            } else if (dna[i] == 'C') {
                comp = 'G';
                
            } else {
                return null;
            }
        
        for (char c : dna)
            return complement1;
        return complement1;
        
    }
    
    public static void main(String[] args) {
        String testData1 = "GCCTGTCGTAGCTTATC",
                testData2 = "GGCTGACGTAGCGTAAC";
        System.out.printf("%s <-- complement --> %s%n", testData1,
                complement(testData1));
        // int[] baseCounts = nucleotideCounts(testData1);
        // System.out.printf("Nucleotide counts for %s: A: %d C: %d G: %d T: %d%n",
        // testData1, baseCounts[0],
        // baseCounts[1], baseCounts[2], baseCounts[3]);
        
        System.out.printf("%s <-- reverse complement --> %s%n",
                testData1, reverseComplement(testData1));
        System.out.printf("%s GC-content: %f%n", testData1,
                gcContent(testData1));
        System.out.printf("Hamming distance between %s and %s: %d%n",
                testData1, testData2,
                hammingDistance(testData1, testData2));
        // System.out.printf("Mutation points between %s and %s:%n%s%n", testData1,
        // testData2,
        // Arrays.toString(mutationPoints(testData1, testData2))); */
    }
}

The code could be simplified by using switch statement for mapping nucleotides.可以通过使用switch语句映射核苷酸来简化代码。

Also the length of the complement was fixed to be the same as that of input dna , and a runtime exception is thrown if the input is invalid.此外, complement的长度固定为与输入dna的长度相同,如果输入无效,则会引发运行时异常。

public static String complement(String dna) {
    
    final int n = dna.length();
    char[] complement = new char[n];
        
    for (int i = 0; i < n; i++) {
        char comp = '\0';
        switch (dna.charAt(i)) {
            case 'A': comp = 'T'; break;
            case 'T': comp = 'A'; break;
            case 'G': comp = 'C'; break;
            case 'C': comp = 'G'; break;
            default:
                throw new IllegalArgumentException("Invalid nucleotide detected at " + i + ": " + dna.charAt(i) + " in DNA: " + dna);
        }
        complement[i] = comp;
    }
    return new String(complement);
}

Test测试

String testData1 = "GCCTGTCGTAGCTTATC",
       testData2 = "GGCTGACGTAGCGTAAC";
System.out.printf("%s <-- complement --> %s%n", testData1, complement(testData1));
System.out.printf("%s <-- complement --> %s%n", testData2, complement(testData2));

Output:输出:

GCCTGTCGTAGCTTATC <-- complement --> CGGACAGCATCGAATAG
GGCTGACGTAGCGTAAC <-- complement --> CCGACTGCATCGCATTG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM