我的程序是將字符串限制在正則表達式[gcatGCAT]中,並將該字符串轉換爲一串互補字符。互補的字符是g到c,c到g,a到u,t到a。我的字符串可以保存或使用多少字符數據?
例如,用戶輸入的字符串「gact」應該產生「cuga」。
但是,當輸入一個長度大於等於50個字符的字符串時(我沒有統計我輸入了多少個字符,只需按住鍵盤上的g和c鍵一會兒。)我想我已經用完了很多我的電腦的RAM,程序凍結,我的操作系統背景改變了。 (我不想嘗試重現它,因爲我很害怕)
我是否在使用太多的內存條來模擬這麼大的字符串並對其執行操作?有什麼辦法可以簡化這個不使用盡可能多的內存,如果這是問題?我真的希望能夠接受200到500個字符的東西,其中1000個是最優的。我是編碼方面的新手,所以如果我錯了,請原諒我。
import java.text.DecimalFormat;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.Random;
//getting my feet wet, 1/13/2015, program is to take a strand of nucleotides, G C A T, for DNA and give
//the complementary RNA strand, C G U A.
public class practiceSixty {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String input = null;
boolean loopControl = true;
char nucleotide;
int repeat;
do{
do{
input = JOptionPane.showInputDialog(null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
}while(!input.matches("[GCATgcat]+")); //uses regular expresions, This method returns true if, and only if, this string matches the given regular expression.
JOptionPane.showMessageDialog(null, "the data you entered is " + input);
StringBuilder dna = new StringBuilder(input);
for(int i = 0; i < input.length(); i++)
{
nucleotide = input.charAt(i);
if(nucleotide == 'G' || nucleotide == 'g')
{
dna.setCharAt(i, 'c');
}
else if(nucleotide == 'C' || nucleotide == 'c')
{
dna.setCharAt(i, 'g');
}
if(nucleotide == 'A' || nucleotide == 'a')
{
dna.setCharAt(i, 'u');
}
else if(nucleotide == 'T' || nucleotide == 't')
{
dna.setCharAt(i, 'a');
}
}
JOptionPane.showMessageDialog(null, "the DNA is , " + input + " the RNA is " + dna);
repeat = JOptionPane.showConfirmDialog(null, "Press Yes to continue, No to quit ", "please confirm", JOptionPane.YES_NO_OPTION);
}while(repeat == JOptionPane.YES_OPTION);
}
});
}
}
請正確縮進您的代碼。 –
我沒有看到你的代碼如何使用50個字符甚至更多....可能是代碼的可讀性可能會提高一點,但對我來說它看起來足夠高效。你的程序實際上是否佔用CPU時間? –
@ValentinRuano我不知道如何確定。我所知道的是,有50個或更多的字符凍結了一下,程序的GUI變黑了。謝謝你。 –