簡體   English   中英

如何在java中將char值添加到char數組

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

我試圖將一個字符的補碼結果放入一個字符數組補碼1中。 我以為我可以使用運算符,但這僅適用於字符串,但我很難弄清楚如何將每個字母放入數組中,以便它可以顯示結果。 任何幫助都會非常感謝。

    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))); */
    }
}

可以通過使用switch語句映射核苷酸來簡化代碼。

此外, 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);
}

測試

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));

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM