简体   繁体   English

给某个数组元素赋值

[英]Assigning a value to a certain array element

I need help with this little piece of code which I did not quite understand while reading a book about Java programming. 我在阅读有关Java编程的书时不太了解的这小段代码需要帮助。 Basically there are some pieces of code which is written for you to correct. 基本上,有些代码是为您纠正而编写的。 I managed to correct some of the training exercises, but this one has made me confused.. 我设法纠正了一些训练方法,但这一方法使我感到困惑。

"The following code segment should assign the value 10 to the array element that corresponds to the third row and the fourth column." “下面的代码段应将值10分配给与第三行和第四列相对应的数组元素。”

int[] a[] = new int[10][5];

a[2][3] = 10;

So this is the code which is written in the book. 这就是书中写的代码。 How is it supposed to look if I actually correct it? 如果我确实纠正了该怎么看? Quite confused of what they actually mean, so if someone could explain I what they mean, I would really appreciate it.. 他们真正的意思很困惑,所以如果有人能解释我的意思,我将非常感激。

Basically there's a typo with the first line, assignment should be: 基本上第一行有一个错字,分配应该是:

int[][] a = new int[10][5];
a[3][2]  = 10;

Your code is incorrect, it should be: 您的代码不正确,应该是:

int[][] a = new int[10][5];
a[3][2]  = 10;

if that is the full question, then it is incomplete. 如果那是完整的问题,那么它是不完整的。 A 2d array can either be row-major or column-major . 2d数组可以是row-majorcolumn-major Without know which it is, then you cannot answer that question. 不知道它是什么,那么您将无法回答该问题。 That is the correct code for a row-major implementation while for column-major you have to flip the indexes. 对于row-major实现,这是正确的代码,而对于column-major row-major实现,则必须翻转索引。

a[3][2] = 10;

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

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