Arrays.toString() 的用法


在做項目的時候很想看一下數組里面具體放的數據,於是我直接打印了數組,但是結果是輸出的居然是數組的地址,咕~~(╯﹏╰)b      

於是查了一下,怎樣才能將數組輸出,找到了Arrays.toString() ,下面是在Arrays類里面這個方法的具體實現方法,我們不用管具體是怎么實現的,只要會使用就可以啦。

 

public class MaoPao2 {
    public static void main(String[] args) {
        int[] nums = {76, 23, 9, 8, 88};
        for (int i = 0; i < nums.length - 1; i++) {//比較的輪數;
            for (int j = 0; j < nums.length - 1 - i; j++) {
                int temp;
                if (nums[j] > nums[j + 1]) {
                    temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                }
            }
        }
        //增強型for
        for (int num : nums) {
            System.out.print("("+nums+"我是nums)\t");
            System.out.print(num + "\t");
        }
    }
}

運行內容:

 

 

 

對比:

使用Arrays.toString():

 

 

運行內容

這個方法是是用來將數組轉換成String類型輸出的,入參可以是long,float,double,int,boolean,byte,object
型的數組。

最后感謝強大的IDEA代碼提醒功能。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM