Results 1 to 8 of 8
  1. #1
    GenkiSudo is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default Should I use StringBuffer?

    I need to make a method that takes a DNA sequence and returns it's complement.

    Complementary sequence means:
    1. replace every 'T' in source sequence with 'A'
    2. replace every 'A' in source sequence with 'T'
    3. replace every 'G' in source sequence with 'C'
    4. replace every 'C' in source sequence with 'G'
    5. reverse the resulting sequence

    Example:
    source =
    "ATGAGGC"
    complementary =
    "GCCTCAT"

    My method looks like this and works with my test strings :
    Java Code:
    public String getComplement(String sequence)
    	{	
    		String complement="";
    		for (int i = sequence.length() - 1 ; i >= 0; i--)
    		{
    			switch (sequence.charAt(i))
    			{
    			case 'a':
    				complement += 't';
    				break;
    			case 't':
    				complement += 'a';
    				break;
    			case 'g':
    				complement += 'c';
    				break;
    			case 'c':
    				complement += 'g';
    				break;	
    			}
    		}
    		
    				
    		return complement;
    	}
    I've overheard my teacher mentioning StringBuffer to other students, but I didn't have time to ask him about it.
    Is there any reason why I should use StringBuffer in this case?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default

    You could use a StringBuffer to build the String that you are going to return (instead of using +=). StringBuffer has methods to add characters to it and to create a String. Read the API doc for more info.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    StringBuilder is recommended since j2se 1.5

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    As @hardwired and @Norm suggest, String is discouraged when you are manipulating the contents, as String is immutable, its better to use StringBuilder or StringBuffer.

    Of course, for a one time program for a homework assignment, it makes no real difference. But as you get better, use a mutable version

  5. #5
    jason wang is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    StringBuffer is used in the condition of there are too many String manipulation. So you should use it in your program, because your sequence is too long.

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by jason wang View Post
    StringBuffer is used in the condition of there are too many String manipulation. So you should use it in your program, because your sequence is too long.
    I see no evidence to agree with your claim that there are "too many" and that the sequence is "too long"

    In general, its likely, but without knowing more than we do, you can't justify your claim that its too many.

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    As far as I know, String and StringBuilder are compiled to a StringBuffer anyways, so it doesn't really matter, regarding speed/memory.
    I die a little on the inside...
    Every time I get shot.

  8. #8
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

Similar Threads

  1. StringBuffer situation
    By orchid in forum New To Java
    Replies: 6
    Last Post: 08-12-2008, 01:39 PM
  2. How to use StringBuffer class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:43 PM
  3. StringBuilder v/s StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 9
    Last Post: 04-11-2008, 09:38 AM
  4. StringBuffer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:33 AM
  5. Help with StringBuffer
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •