[英]Array not printing the value of index, printing address instead
Hookay,所以,我無法理解這個問題。 說實話,陣列激怒並迷惑我。 ......這是學習的一部分,但是對嗎?
所以這是我的問題
Netbeans輸出這個-_-
這是代碼
//////Problem 4////////
System.out.println("Please Enter the Size of your array");
int arraysize = in.nextInt();
//initalize array
int [][] aOne = new int[arraysize][arraysize];
// load array 1
for (int i = 0; i< aOne.length; i++){
for(int x = 0; x <aOne[i].length;x++){
aOne[i][x] = (int)(Math.random()* 15);}}
//print aOne
for (int i = 0; i< aOne.length; i++){
for (int x = 0; x<aOne.length; x++){
System.out.print(aOne[i]+" "+aOne[x]);
}
System.out.println();
}
到底是怎么回事?
編輯:我明白它給了我內存位置...為什么不打印出數字? 抱歉。 標題是我書中的問題,我一般都遇到陣列問題
換行:
System.out.print(aOne[i]+" "+aOne[x]);
至
System.out.print(aOne[i][x]+" ");
然后你會得到數字。 否則,您將獲得2D陣列的行[i]和[x]的記憶地址。
如果要打印int []數組。
使用aOne [i] [x]在int []數組中打印指定的元素。
另外,對於打印int []數組的第二個for循環,你最好使用x <aOne [i] .length instaed of x <aOne.length ,因為它會在行數和數字時引起問題int []數組中的列不同。
用這個做一些,
從
//print aOne
for (int i = 0; i< aOne.length; i++){
for (int x = 0; x<aOne.length; x++){
System.out.print(aOne[i]+" "+aOne[x]);
}
System.out.println();
}
至
//print aOne
for (int i = 0; i< aOne.length; i++){
for (int x = 0; x<aOne[i].length; x++){
System.out.print(aOne[i][x]+" ");
}
System.out.println();
}
控制台中的一個運行結果如下:
5 0 10
1 11 8
7 7 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.