简体   繁体   English

数组不打印索引值,而是打印地址

[英]Array not printing the value of index, printing address instead

Hookay, so, I'm having trouble understanding this problem. Hookay,所以,我无法理解这个问题。 To be completely honest, arrays infuriate and confuse me. 说实话,阵列激怒并迷惑我。 ...That's part of learning though right? ......这是学习的一部分,但是对吗?

So here's my problem 所以这是我的问题

Netbeans is outputting this -_- Netbeans输出这个-_- wtf netbeans

Here be the code 这是代码

//////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();
  }

What is going on? 到底是怎么回事?

edit: I understand it's giving me the memory locations... why isn't it printing out the numbers? 编辑:我明白它给了我内存位置...为什么不打印出数字? Sorry. 抱歉。 The title is the question in my book, I'm having trouble with arrays in general 标题是我书中的问题,我一般都遇到阵列问题

Change the line: 换行:

System.out.print(aOne[i]+" "+aOne[x]);

to

System.out.print(aOne[i][x]+" ");

then you will get the numbers. 然后你会得到数字。 Otherwise you get the memorie adress of the row [i] and [x] of your 2D array. 否则,您将获得2D阵列的行[i]和[x]的记忆地址。

If you want to print int[] array. 如果要打印int []数组。

Use aOne[i][x] to print the specified element in int[] array. 使用aOne [i] [x]在int []数组中打印指定的元素。

In addition, for the second for-loop for printing int[] array, you'd better using x < aOne[i].length instaed of x < aOne.length , since it will cause issue when the number of rows and the numbers of columns in int[] array are different. 另外,对于打印int []数组的第二个for循环,你最好使用x <aOne [i] .length instaed of x <aOne.length ,因为它会在行数和数字时引起问题int []数组中的列不同。

Make some some with this, 用这个做一些,

from

//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();
}

to

      //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();
      }

One run result in console is as follows: 控制台中的一个运行结果如下:

5 0 10 
1 11 8 
7 7 5 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM