Java Array Output

Source: Internet
Author: User
Tags textout

Java Array Output

The Java array output is typically looped out, for example (CODE1):

int[] arr = new Int[10];for (int i = 0; i < arr.length; i++) {System.out.print (arr[i]);}

however, for arrays of type char[], you can use an output statement output, for example (CODE2):

Char[] CHS = {' A ', ' B ', ' C ', ' d '}; System.out.println (CHS);
note the differences between the two print statements in Code1 and Code2:

System.out.print (Arr[i]);//code1system.out.println (CHS);//code2
Print int array arr does not wrap because it is System.out.print (); When printing a char array chs, there is no line break in the middle, as if the CHS array content is printed as a string.


The reason that the char array can be directly exported is because of the implementation in the source code:

void Java.io.PrintStream.println (char[] x)

    /**     * Prints An array of characters and then terminate the line.  This method     * behaves as though it invokes <code>{@link #print (char[])}</code> and     * then <code> ; {@link #println ()}</code>.     *     * @param x an  array of chars to print.     */Public    void println (char x[]) {        synchronized (this) {            print (x);            NewLine ();        }    }

void Java.io.PrintStream.print (char[] s)

    /**     * Prints an array of characters.  The characters is converted into bytes     * According to the platform ' s default character encoding, and these     bytes * is written in exactly the manner of the     * <code>{@link #write (int)}</code> method.     *     * @param      s The array of chars to be   printed     *     * @throws  nullpointerexception  If < Code>s</code> is <code>null</code>     * * Public    void print (char s[]) {        write (s);    }

void Java.io.PrintStream.write (char[] buf)

/* * The following private methods on the Text-and character-output streams * A Lways flush the stream buffers, so this writes to the underlying byte * stream occur as promptly as with the original     PrintStream.                */private void Write (char buf[]) {try {synchronized (this) {Ensureopen ();                Textout.write (BUF);                Textout.flushbuffer ();                Charout.flushbuffer ();                            if (AutoFlush) {for (int i = 0; i < buf.length; i++) if (buf[i] = = ' \ n ')                Out.flush ();        }}} catch (Interruptedioexception x) {Thread.CurrentThread (). interrupt ();        } catch (IOException x) {trouble = true; }    }
TextOut is an BufferedWriter object that represents the output stream object that outputs information to the console. Charout is a OutputStreamWriter object, which is a transform stream object used to convert bytes into characters. TextOut packed the Charout
Textout.write (BUF); Call to the following method:

    /**     * Writes an array of characters.     *     * @param  cbuf     *         Array of characters to be written     *     * @throws  ioexception     *          If an I/O error occurs */public    void write (char cbuf[]) throws IOException {        write (cbuf, 0, Cbuf.len gth);    }
the method is to output each character of a char array to the console one at a time.


Summing up a sentence, in fact System.out.println (c); is to output each character of the C array to the console one by one.

Java Array Output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.