這裏是我的代碼:打印字符串數組
String[] magic = {"stick", "hat", "witch"};
String magic1 = magic.toString();
String magic2 = Arrays.toString(magic);
System.out.println(magic1); // this is printing a memory location
System.out.println(magic2); // this one prints: [stick, hat, witch]
是什麼magic1
和magic2
之間的區別?
'magic1'是數組的默認toString()輸出。 'magic2'是Arrays類中toString()方法的輸出,它可以獲取所有元素。 – csmckelvey
首先打印出陣列魔法的身份散列碼。可能看起來像'String @ 548543fdfds'。 –
除了他不問如何打印數組。 –