Java Fundamentals Hardening 105: A summary of methods for printing arrays

Source: Internet
Author: User
Tags tojson

1. Use the For loop to print the set of impressions.

2. Use the Arrays tool class to print the array into an ordered list.

3. Use the Arrays tool class to output the array contents using Arrays.tostring ().

The example code for the above three ways to print an array is as follows:

 PackageCom.himi.printarray;Importjava.util.Arrays; Public classAnormalarray { Public Static voidMain (string[] args) {/*** Print group with for Loop */String names[]= {"Georgianna", "Tenn", "Simon", "Tom" }; System.out.print ("[");  for(inti=0; i<names.length; i++) {            if(i== names.length-1) {System.out.print (Names[i]+"]"); } Else{System.out.print (names[i]+", ");                 }} System.out.println (); /*** use Arrays to convert an array to an ordered List print out * <String> list<String> Arrays.aslist (
     
      string. A)
     */System.out.println (arrays.aslist (names)); /*** use arrays.tostring to output array contents */System.out.println (arrays.tostring (names)); }}

Run the results as follows:

4. Use the Arrays tool class to print a 2-D array using arrays.deeptostring.

code example:

 PackageCom.himi.printarray;Importjava.util.Arrays;/** print array with arrays.deeptostring    */ Public classPrintArray { Public Static voidMain (string[] args) {//2d Array, need arrays.deeptostringstring[][] Deeparraystr =NewString[][] {{"Yiibai1", "Yiibai2"}, {"Yiibai3", "Yiibai4" } }; //Output: [[ljava.lang.string;@15db9742, [ljava.lang.string;@6d06d69c]System.out.println (arrays.tostring (DEEPARRAYSTR)); //Output: [[Yiibai1, Yiibai2], [Yiibai3, Yiibai4]]System.out.println (arrays.deeptostring (DEEPARRAYSTR)); //2d Array, need arrays.deeptostring        int[] Deeparrayint =New int[] {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10 } }; //Output: [[email protected], [[email protected]]System.out.println (arrays.tostring (deeparrayint)); //Output: [[1, 3, 5, 7, 9], [2, 4, 6, 8, ten]]System.out.println (arrays.deeptostring (deeparrayint)); }}

5. Using the tool class Gson (JSON parsing package developed by Google), convert the array to a JSON string to print out.

The code is as follows:

 PackageCom.himi.printarray;ImportCom.google.gson.Gson;/*** Print a JSON-formatted string. * @authorHebao **/ Public classPrintutils { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intNumbers[] = {1,2,3,4,5,6,7}; String[] days= {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; Gson Gson=NewGson (); String Numbersjson=Gson.tojson (numbers); String Daysjson=Gson.tojson (days); System.out.println ("numbers arrays into JSON data: "+Numbersjson); System.out.println ("Days array into JSON data: "+Daysjson); System.out.println (""); System.out.println ("-------Convert the JSON string to a string array-------"); String[] Weekdays= Gson.fromjson (Daysjson, string[].class);  for(inti=0; i<weekdays.length; i++) {            if(i = = Weekdays.length-1) {System.out.print (weekdays[i]); } Else{System.out.print (weekdays[i]+ ","); }} System.out.println (""); System.out.println ("-------Convert a multidimensional int array to a JSON-------"); int[] data = {{1, 2, 3}, {3, 4, 5}, {4, 5, 6}}; String JSON=Gson.tojson (data); System.out.println ("Data =" +JSON); System.out.println ("-------Convert the JSON string group into a multidimensional int array-------"); int[] DataMap = Gson.fromjson (JSON,int[][].class);  for(inti = 0; i < data.length; i++) {             for(intj = 0; J < Data[i].length; J + +) {System.out.print (Data[i][j]+ " "); } System.out.println (""); }    }}

The results of the program run as follows:

6. Using the stream to print an array, we can convert the array to a stream and print it out.

code example:

 PackageCom.himi.printarray;Importjava.util.Arrays;/** Use stream to print an array * We can convert it to a stream and print it out. */ Public classStreamarray { Public Static voidMain (string[] args) {//Arraystring[] Arraystr =Newstring[]{"Java", "Node", "Python", "Ruby"};        Arrays.stream (ARRAYSTR). ForEach (System.out::p rintln); int[] Arrayint = {1, 3, 5, 7, 9};        Arrays.stream (Arrayint). ForEach (System.out::p rintln); //2d Arraystring[][] Deeparraystr =Newstring[][]{{"Yiibai1", "Yiibai2"}, {"Yiibai3", "Yiibai4"} }; Arrays.stream (DEEPARRAYSTR). FLATMAP (x-Arrays.stream (x)). ForEach (System.out::p rintln); int[] Deeparrayint =New int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}; Arrays.stream (Deeparrayint). Flatmaptoint (x-Arrays.stream (x)). ForEach (System.out::p rintln); }}

The results of the program run as follows:

Java Fundamentals Hardening 105: A summary of methods for printing arrays

Related Article

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.