Reputation: 43
This is my first time asking a question here, so forgive me if my formatting is a little sloppy.
I need to know how to set all the elements of a 2D Array to the same value, using for loops, with Java.
I was able to get this far:
import java.util.Arrays;
public class TheArray{
public static void main (String[] args){
int[][] pixels = new int[768][1024];
for(int i = 0; i < pixels.length; i++){
for(int j = 0; j < pixels[i].length; j++){
Arrays.fill(pixels[i], 867);
}
}
}
}
However I have no idea if I have done it right. So all I want it to do is make all of the elements of the 2D array be 867.
Upvotes: 1
Views: 2769
Reputation: 32343
You don't need to use Arrays.fill
if you're already using for
loops. Just do:
for(int i = 0; i < pixels.length; i++){
for(int j = 0; j < pixels[i].length; j++){
pixels[i][j] = 867;
}
}
Arrays.fill
would be used instead of the inner for
loop.
Upvotes: 4
Reputation: 16429
Use the following line inside your loop:
pixels[i][j] = 867;
Upvotes: 0