• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to append the contents of an ArrayList  RSS feed

 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day!

I have this code snippet:


Once the ArrayList stops collecting data, I want its contents to be
concatenated or appended to each other and then store it in a
String variable.

Thanks.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use ArrayList<String> but ArrayList<Byte> instead. Then cast bytesRead to a byte* and add that byte directly - autoboxing will make sure that the byte will be converted to a Byte, while caching all the Byte objects.
Then afterwards create a byte[] of the size of the list, iterate through the list and set the right values. Finally, create a String object using this byte array.

* InputStream.read() will return the byte value between 0 and 255. However, when 255 is returned the actually byte value is -1; likewise for 128 and -128. Casting the integer to a byte will result in that actual byte value again.
The reason why only values between 0 and 255 are returned is that this allows -1 to be used as the end marker.



Of course you can also use a better strategy: wrap the InputStream in an InputStreamReader, create a StringWriter, copy all the chars from the InputStreamReader directly to the StringWriter, and once done use the StringWriter's toString method to get the string:

You could also play around with using the other read and write methods; sometimes using the versions with byte[] or char[] will be faster than the byte-per-byte or char-per-char reading.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code snippet, I have tried it and it worked.
However, when I debugged the program, I only get one character at a time
I want to store this characters in an Array then convert it to a
string.
And when I added this:



so that I can see its contents, the program doesn't "go" there.

Here's the code:


[ October 17, 2007: Message edited by: Darren Alexandria ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darren Alexandria:
Thanks for the code snippet, I have tried it and it worked.
However, when I debugged the program, I only get one character at a time
I want to store this characters in an Array then convert it to a
string.
And when I added this:



so that I can see its contents, the program doesn't "go" there.

If it doesn't reach this statement, it means that the input stream still has not reached the end. Now I haven't worked with serial ports yet, but it could be possible that it will never reach the end as long as the serial port is available.

If this is the case you will have to know what to read, and read exactly the number of bytes/chars you need, always taking into consideration that read() still might return -1. You'd have to have a counter that counts the number of bytes/chars. After each byte/char read you increase the counter, and if it has reached the number you need you break out of the loop.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

The problem is that the data coming from the machine doesn't have a fixed length. That is why I am really having a difficult time working on this.

My plan now is this:
since I am getting the data per character, I will place or store it in a
character array. Then I will convert that char array into a string.

However, getting the data from the Input Stream the directly storing it to
a character array is also my option. After the contents have been loaded up,
then I will convert it into a String.

Which is better and more feasible to do?

Thanks again.

[ October 17, 2007: Message edited by: Darren Alexandria ]
[ October 17, 2007: Message edited by: Darren Alexandria ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!