Results 1 to 4 of 4
  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Manual array copy wont work

    While experimenting with my own StringBuilder class, I'v noticed that manual array copy in code below wont work(line36-37), and wonder why?
    But if used System.arraycopy method(line 35), everything goes fine.
    Also line above this problematic manual array copy works fine(line32-33).

    Here is the code:
    Java Code:
    public class MyStringBuilder2 {
        private int size = 0;
        private int capacity = 0;
        private char[] buffer;
        
        public MyStringBuilder2() {
            
        }
        
        public MyStringBuilder2(char[] chars) {
            int len = chars.length;
            buffer = new char[len];
            System.arraycopy(chars, 0, buffer, 0, len);
            size = capacity = len;
        }
        
        public MyStringBuilder2(String s) {   
            int len = s.length();
            buffer = new char[len];
            size = capacity = len;
            for (int i = 0; i < len; i++)
                buffer[i] = s.charAt(i);
        }
        
        public MyStringBuilder2 insert(int offset, MyStringBuilder2 s) {
            if (capacity < size + s.size) {
                int newCapacity = 2 * (size + s.size);
                increaseCapacity(newCapacity);
            }                                 
            //0123456789__
            //012__3456789       
            for (int i = size + s.size - 1; i > offset + s.size - 1; i--)
                buffer[i] = buffer[i - s.size];
                            
            //System.arraycopy(s.buffer, 0, buffer, offset, s.size);//works fine
            for (int i = 0; i < s.size; i++)
                buffer[offset + i] = s.buffer[i];
            
            size += s.size;
            return null;
        }
        
        private void increaseCapacity(int newCapacity) {
            char[] temp = new char[newCapacity];
            System.arraycopy(buffer, 0, temp, 0, size);
            capacity = newCapacity;
            buffer = temp;
        }
    }
    Last edited by diamonddragon; 01-27-2012 at 07:50 PM.

  2. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Manual array copy wont work

    This is tester:
    Java Code:
    public class TestMyStringBuilder2 {
        public static void main(String[] args) {
            MyStringBuilder2 msb1 = new MyStringBuilder2("123456789");
            MyStringBuilder2 msb2 = new MyStringBuilder2("XX");
            MyStringBuilder2 msb3 = msb1.insert(3, msb2);        
        }
    }

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Manual array copy wont work

    And now debuger says its working fine, but who will believe me this was not going fine, and lines 36-37 wont copy properly into buffer array?

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Manual array copy wont work

    While in the same thread, another question is, is this append method for the same class above, ok?
    Or it should be approached in some other way?

    Java Code:
        public MyStringBuilder2 append(MyStringBuilder2 s) {                        
            String s1 = new String(buffer);
            String s2 = new String(s.buffer);        
            return new MyStringBuilder1(s1 + s2);
        }
    Last edited by diamonddragon; 01-27-2012 at 08:35 PM.

Similar Threads

  1. Why Wont This Work???
    By Billywizz in forum New To Java
    Replies: 11
    Last Post: 03-09-2011, 03:33 AM
  2. why wont the %.2f work here?
    By jjth39347 in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 06:55 AM
  3. importing JCurses wont work!
    By TheBreadCat in forum New To Java
    Replies: 0
    Last Post: 02-16-2011, 09:46 PM
  4. JApplet buttons wont work
    By Mark101 in forum New To Java
    Replies: 3
    Last Post: 03-16-2010, 07:53 PM
  5. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 05:19 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
  •