aspose file tools
The moose likes Beginning Java and the fly likes how to append the contents of an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "how to append the contents of an ArrayList" Watch "how to append the contents of an ArrayList" New topic
Author

how to append the contents of an ArrayList

Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
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

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
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

Joined: Oct 27, 2005
Posts: 19216

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

Joined: Aug 17, 2007
Posts: 185
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 ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: how to append the contents of an ArrayList
 
Similar Threads
Copying Multiple Files Over A Socket
printing...static context?
Submit button-simple question
Iterating List of Maps through c:foreach
Write a String to ByteArrayOutputStream