Reputation: 81
I am trying to get a number from user input to be the row number of the 2d array already existed and another number to be the value that needs to be added to the elements of that row. I have no idea how to do it. Please help me and give me some idea where to start.
So for example if i already have my 2d array and the content is:
2 3 4 5 1
2 6 2 5 6
4 2 6 2 1
I know how to get the numbers from the user input and locate that row but I just don't know how to add the second number to the elements of that row.
For example:
I need to do 2+2 3+2 4+2 5+2 1+2 and save the row zero back to the 2d array. How can I do it?
Upvotes: 1
Views: 277
Reputation: 162771
If you're simply stuck on the 2d array syntax, it's something like this:
myArray[0][0] = myArray[0][0] + 2;
myArray[0][1] = myArray[0][1] + 2;
myArray[0][2] = myArray[0][2] + 2;
myArray[0][3] = myArray[0][3] + 2;
myArray[0][4] = myArray[0][4] + 2;
or more concisely:
for (int i=0, length=myArray[0].length; i<length; i++) {
myArray[0][i] += 2;
}
Upvotes: 1
Reputation: 1246
for (int i = 0; i < array[selected].length; i++)
array[selected][i] += valueToAdd;
a 2d array is an array of arrays, so array[selected]
is of type int[]
Upvotes: 0