Top.Mail.Ru
February 29th, 2004 - Java developers — LiveJournal
? ?

Java developers

February 29th, 2004

01:06 pm - dplass - Here's another one

Is there anything wrong with this code?
    public static byte[] serialize(Serializable ob) throws IOException {
        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();

        // Create a briefly existing object output stream to write the object
        // into an array of bytes.
        new ObjectOutputStream(byteOutStream).writeObject(ob);

        return byteOutStream.toByteArray();
    }


Specifically, the ObjectOutputStream -- will that get closed and GC'ed properly? I seem to remember some kind of JDK resource "leak" with code like this, whereby a temporary object is not cleaned up right. Is this true?

Alternatively, can anyone suggest a better way to serialize something to an array of bytes?
 

04:24 pm - mgrjoe - Triangle

Hey guys & girls :)
I am trying to make a star program which will output a neat triangle. Where if the user inputs "3" it will make a triangle with a length of 3, as seen below. Now I made a simple program that will print out something similar but it's missing the indents and what not. I can see where the measurments of the length, be it 3... the top needs to be spaced 2, then the next 1 then the last 0... how would I go about this? Thank you :)
JoeY
  *
 ***
*****


public class StarProgram
{
        public static void main (String[] args)
        {
                System.out.print("Enter a number: ");
                int max = SavitchIn.readLineInt();
                        for (int row = 1; row <= max; row++)
                        {
                        for (int star = 1; star <= row; star++)
                        System.out.print ("*");

                        System.out.println();
                        }
        }
}
Powered by LiveJournal.com