2014-10-06 39 views
0

我正在嘗試完成這個程序的作業,這是唯一不起作用的東西。我在這一行上得到一個預期的錯誤:使用ArrayList時無效的標識符<Character>

System.out.println(myChars.equals(complement));

這裏是我的代碼

public class WCpalindrome { 
    private static char[] dna = {'A', 'C', 'T', 'G'}; 
    private static ArrayList<Character> myChars = new ArrayList<Character>(); 
    private static ArrayList<Character> reverse = new ArrayList<Character>(); 
    private static ArrayList<Character> complement = new ArrayList<Character>(); 


public static char toUpperCase(char c) { 
    if (c == 'A' || c == 'a') { 
    return 'A'; 
    } else if (c == 'T' || c == 't') { 
    return 'T'; 
    } else if (c == 'C' || c == 'c') { 
    return 'C'; 
    } 
    return 'G'; 
} 

public static char getComplement(char c) { 
    if (c == 'A' || c == 'a') { 
    return 'T'; 
    } else if (c == 'T' || c == 't') { 
    return 'A'; 
    } else if (c == 'C' || c == 'c') { 
    return 'G'; 
    } 
    return 'C'; 
} 

public static void main(String[] args) { 

    char current; 
    int i = 0; 

    //Get the input sequence 
    while (StdIn.hasNextChar()) { 
    current = StdIn.readChar(); 
    current = toUpperCase(current); 
    myChars.add(current); 
    StdOut.println(myChars.get(i)); 
    i++; 
    } 

    System.out.println(); 

    //Reverse the sequence 
    int k = 0; 
    int size = myChars.size() - 1; 
    for (int j = size-1; j >= 0; j--) { 
    current = myChars.get(j); 
    StdOut.println(current); 
    reverse.add(k, current); 
    k++; 
    } 

    System.out.println(); 

    //Complement the reversed sequence 
    int n = 0; 
    size = myChars.size() - 1; 
    for (n = 0; n < size; n++) { 
    current = reverse.get(n); 
    complement.add(getComplement(current)); 
    StdOut.println(complement.get(n)); 
    } 
} 

//Prints true if the original equals the complement of the reversal 
//else it prints false 
System.out.println(myChars.equals(complement)); 

}

+0

何時該行被執行? – 2014-10-06 15:04:57

回答

3

你所談論的線沒有任何方法內。我認爲你想在你的主要方法中移動它。

-1
System.out.println(myChars.equals(complement)); 

以外的主要方法。

本應在年底

} 
//Prints true if the original equals the complement of the reversal 
    //else it prints false 
    System.out.println(myChars.equals(complement)); 
} 
} 
0

你有地方的聲明System.out.println(...);main()。將它移動3行。它現在處於方法和現場聲明預期的水平。

此外等於總是會返回false,因爲數組實例在內部元素中沒有比較。 One might use Arrays.equals`但你可能會簡單地使用:

System.out.println(new String(myChars).equals(new String(complement)));