Shop OBEX P1 Docs P2 Docs Learn Events
Buffer Mania — Parallax Forums

Buffer Mania

trytryagaintrytryagain Posts: 26
edited 2009-10-06 21:31 in General Discussion
Peter,

This one is VERY STRANGE.

I am trying to create a list of transactions

static List scanLog = new List(100);

static StringBuffer myBuffer = new StringBuffer();··

1. First I use a string buffer to create a transaction string
···then add this string to a list

myBuffer.clear();
myBuffer.append("@");
myBuffer.append("123");
scanLog.add(0,myBuffer.toString());·

2. Now I clear myBuffer, Create a second string and store
··· the next string in the list as a new index

myBuffer.clear();
myBuffer.append("@");
myBuffer.append("456");
scanLog.add(1,myBuffer.toString());·

3. Now I look at whats in the list by index

System.out.println((String)scanLog.get(0));
System.out.println((String)scanLog.get(1));

This reports:
@456
@456

It some how holds a reference to last myBuffer string
and doesn't hold the correct strings in the List. Bizzare.


The Strange part is when I do not use a stringbuffer
it seems to work:

String str = "@123"
scanLog.add(0,str);

str = "@456"
scanLog.add(1,str);

System.out.println((String)scanLog.get(0));
System.out.println((String)scanLog.get(1));

Now I get:
@123
@456

My problem is I need to use a stringbuffer to
assemble my strings for memory efficiency.

What don't I get about these stupid stringbuffers
I am clearing it and it still won't work properly.

PLEASE HELP!

PS: Even when I create a string array -vs- string buffer
I get the same result when I set it equal to a string buffer
I am reusing with myBuffer.clear(); (I've also tried myBuffer.delete(0,4))


Jeff(trytryagain)










·

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-10-06 16:00
    Remember that StringBuffer only uses one character array.
    Applying toString merely returns the reference to this one array, but as type String.

    So
    1. First I use a string buffer to create a transaction string
    ···then add this string to a list

    myBuffer.clear();
    myBuffer.append("@");
    myBuffer.append("123");
    scanLog.add(0,myBuffer.toString());·

    2. Now I clear myBuffer, Create a second string and store
    ··· the next string in the list as a new index

    myBuffer.clear();
    myBuffer.append("@");
    myBuffer.append("456");
    scanLog.add(1,myBuffer.toString());·

    3. Now I look at whats in the list by index

    System.out.println((String)scanLog.get(0));
    System.out.println((String)scanLog.get(1));

    This reports:
    @456
    @456

    makes perfect sense as you store the references, not the content of the array.


    When using Strings,

    String str = "@123"
    scanLog.add(0,str);

    str = "@456"
    scanLog.add(1,str);

    System.out.println((String)scanLog.get(0));
    System.out.println((String)scanLog.get(1));

    Now I get:
    @123
    @456

    str gets the reference of the constant strings and those references are different,
    and thus you store different references. and thus reading back shows different content.


    What you need to do is to store the content of the StringBuffer in a new StringBuffer,
    and store the reference of this new StringBuffer.

    regards peter
  • trytryagaintrytryagain Posts: 26
    edited 2009-10-06 16:13
    Pete,

    Can I create an array of 100 string buffers

    each with a max length of 30

    up front so I know my memory allocation.



    If so, could you do me big favor and show me

    the proper declaration for the string buffer array

    and change my supplied code as an example.

    I have·been working on this for over 6 hours.

    Thanks as always Pete,

    Jeff
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-10-06 16:26
    Here you go

    import stamp.core.*;
    
    public class StringBufferList_test {
    
      static StringBuffer sArray[noparse][[/noparse]] = new StringBuffer[noparse][[/noparse]100];
      static {
        for (int i=0; i<100; i++) {
          sArray[noparse][[/noparse]i] = new StringBuffer(30);
        }
      }
    
      static void main() {
      }
    
    }
    


    regards peter
  • trytryagaintrytryagain Posts: 26
    edited 2009-10-06 17:25
    mad.gif·Pete,

    I initialized the buffer per your example:

    static StringBuffer sArray[noparse]/noparse = new StringBuffer[noparse][[/noparse]100];
    · static {
    ··· for (int i=0; i<100; i++) {
    ····· sArray[noparse][[/noparse]i] = new StringBuffer(30);
    ··· }
    · }

    myBuffer.clear();
    myBuffer.append("@");
    myBuffer.append("123");

    int scanIndex = 0;

    scanIndex++;
    sArray[noparse][[/noparse]scanIndex] = myBuffer;

    myBuffer.clear();
    myBuffer.append("@");
    myBuffer.append("456");

    scanIndex++;
    sArray[noparse][[/noparse]scanIndex] = myBuffer;

    I am still getting the same values for
    sArray[noparse][[/noparse]1] = @456
    sArray[noparse][[/noparse]2] = @456

    I am using a different index of the sArray buffer
    each time and Its still happening!

    Jeff




    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-10-06 18:55
    Do it like this:

    static StringBuffer sArray[noparse]/noparse = new StringBuffer[noparse][[/noparse]100];
    · static {
    ··· for (int i=0; i<100; i++) {
    ····· sArray[noparse][[/noparse]i] = new StringBuffer(30);
    ··· }
    · }

    int scanIndex = 0;

    sArray[noparse][[/noparse]scanIndex].clear();
    sArray[noparse][[/noparse]scanIndex].append("@");
    sArray[noparse][[/noparse]scanIndex].append("123");

    scanIndex++;

    sArray[noparse][[/noparse]scanIndex].clear();
    sArray[noparse][[/noparse]scanIndex].append("@");
    sArray[noparse][[/noparse]scanIndex].append("456");

    scanIndex++;
    etc.

    Better to create a method clear and append

    static void clear(int i) {
    · sArray[noparse][[/noparse]i].clear();
    }

    static void append(int i, String s) {
    · sArray[noparse][[/noparse]i].append(s);
    }

    and call these methods

    regards peter
  • trytryagaintrytryagain Posts: 26
    edited 2009-10-06 19:08
    Pete

    Does that work because the reference is coming from a function?

    Jeff
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-10-06 19:15
    It works because you use another StringBuffer each time you
    change the index.
    The nice thing about a OOP language like java is
    that you can call a method via the variable.
    sArray[noparse][[/noparse]i].append(s);
    sArray[noparse][[/noparse]i] is the variable (of type StringBuffer in this case), append(s) is the method.
    And that also allows it to be embedded in another method.

    regards peter
  • trytryagaintrytryagain Posts: 26
    edited 2009-10-06 19:24
    cool.gif·Pete,

    After all this I have really grown to dislike strings

    These references backword certainly save on space

    but make it frustrating to use strings in logs.

    Luckily all the values in my string are integers

    so I made arrays out of each piece:

    index++

    Month[noparse][[/noparse]index] = 9

    Day[noparse][[/noparse]index] = 14

    Year[noparse][[/noparse]index] = 2009

    index++

    Month[noparse][[/noparse]index] = 10

    Day[noparse][[/noparse]index] = 25

    Year[noparse][[/noparse]index] = 2009

    With integers the values stick.

    I assemble the same buffer each time I send a different index of data

    buff.clear();

    int indextosend = 0;

    buff.append(Month[noparse][[/noparse]indextosend]);

    buff.append(Day[noparse][[/noparse]indextosend]);

    buff.append(Year[noparse][[/noparse]indextosend]);

    I will try your suggestion as well Pete

    Thanks so much for the explanation

    I hope in future versions of Javelin they

    give us a little more memory and a better

    method to work with strings.

    Jeff(trytryagain)
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-10-06 20:08
    Since you are dealing with dates and times,
    check out my Calendar class. It provides methods
    to convert between binary stored dates and times
    and human readable strings.

    regards peter
  • trytryagaintrytryagain Posts: 26
    edited 2009-10-06 21:31
    smile.gif·Peter,

    I appreciate the·calendar code.

    Thanks so much for all your help today.

    Jeff
Sign In or Register to comment.