Home  >  Article  >  Java  >  How to print array using toString() method in Java?

How to print array using toString() method in Java?

PHPz
PHPzforward
2023-05-09 10:01:071608browse

1. Description

The first function can be converted into a string

The second function can be used to convert the numerical value into a string of different base numbers (octal, decimal, etc. )

2. Syntax

String toString()
static String toString(int i)

3. Parameters

i -- The integer to be converted.

4. Return value

toString(): Returns a String object representing an Integer value.

toString(int i): Returns a String object representing the specified int.

5. Example

import java.util.Arrays;
 
public class ArrayPrint {
public static void main(String[] args){
//第一种方式:
    int a[][]={{1,2,3},{4,5,6}};
    System.out.println("a:"+Arrays.toString(a));
    
    int b[][]={{1,2,3},{4,5,6}};
    System.out.println("b:"+Arrays.deepToString(b));
    
    //第二种方式;
    //int[][] ints = new int[4][2];        
    //ints[i][j] =__; //分别赋值
    
    //第三种方式:第二维的长度可以动态申请
    int[][] arr3 = new int[5][];       //五行的长度
    for(int i=0; i<arr3.length; ++i){      
        arr3[i]=new int[i+1];         //列的长度每次都变化。每次都要重新申请空间(长度)
        for(int j=0; j<arr3[i].length; ++j){
            arr3[i][j]= i+j;  
            System.out.println( "arr3[i][j]:"+ arr3[i][j]);
        }
        
        System.out.println( "arr3[i][j]:"+ Arrays.toString(arr3[i]) );
    }
 }
}

The above is the detailed content of How to print array using toString() method in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete