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

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

PHPz
Release: 2023-05-09 10:01:07
forward
1655 people have browsed it

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)
Copy after login

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]) );
    }
 }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template