We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hey guys I'm supposed to make this 2D array and so far i've got the values in but the ends are being filled with zeroes. Here's the instructions
and here's my code:
void setup()
{
int [][] arr= new int[6][6];
arr[0][0]=0;
arr[0][1]=1;
arr[0][2]=0;
arr[0][3]=1;
arr[0][4]=1;
arr[1][0]=1;
arr[1][1]=0;
arr[1][2]=1;
arr[1][3]=0;
arr[2][0]=0;
arr[2][1]=1;
arr[3][0]=1;
arr[3][1]=0;
arr[3][2]=1;
arr[3][3]=1;
arr[3][4]=0;
arr[3][5]=0;
arr[4][0]=0;
arr[4][1]=0;
arr[4][2]=1;
arr[5][0]=1;
arr[5][1]=1;
arr[5][2]=1;
arr[5][3]=0;
arr[5][4]=0;
for (int i=0; i<arr.length; i++)
{
for (int j=0; j<arr.length; j++)
{
print(arr[i][j]);
}
println();
}
}
Answers
Which indexes are different from what you expect? Which lines are you initializing those indexes on?
If I were you, I would get out a piece of paper and a pencil. Draw a grid that shows the indexes and the value you want in that index in each cell. Then look at your code and see where you're setting those indexes.
@ENGG233 --
Your
new int[6][6]
will be made of ints (6x6 of them), and ints each have a default value of 0 -- see the Default Values section of Java datatypes.Then you explicitly make some of them 0s and 1s. The rest are still 0s.
So either:
If they need to be different sizes you cannott use the [][] syntax. You want to create a two-dimensional array like:
...and then add your rows (each a different length) one at a time.
I would think of a 2D array as a table, not an array of list objects (Referring to the initial post). @GoToLoop How is it possible for Processing to tell the end of a row? I mean, conceptually all arrays would be coded internally as 1D arrays if I remember correctly?
Kf
It's got nothing to do w/ Processing, which isn't an actual programming language, but w/ the Java language. ~O)
The outer array stores the memory addresses (references or pointers) of each inner array object.
And each inner array stores
byte
primitive values. :-B@kfrajer
It is actually a mistake to think of a 2D array like a grid or a checkerboard. It is rail-with-rods -- more like a set of vertical blinds, or like a newspaper rack.
When you use [x][y] notation then all the rows start out with the same length y. However, nothing structural is forcing them to remain that way -- it isn't a special rectangular-shaped kind of data, it is still just a rail with rods attached. Let's adapt GoToLoop 's example to make a 3x3, then replace the second row.
Output:
the arrays are special in JAVA though.
in other programming languages arrays are different conceptually and indeed like a grid (e.g. in Basic)
In Java, arrays are dynamically allocated:
https://en.Wikipedia.org/wiki/Memory_management#DYNAMIC
And multiple arrays are jagged. That is, they can have different lengths:
https://en.Wikipedia.org/wiki/Jagged_array
Nope! Dynamically jagged arrays are the most common among programming languages.
Rectangular arrays, like those used in C & BASIC, are much uncommon. :P
;-)
Thanks everyone for your responses. So i got the print out, but now I need make this pattern out of it. It's not workingggg :( I'm basically trying to use nested for loops to go through each row and all columns for that row and make the rectangles for it. What it should look like:
what I got going on:
use rect after the fills
use rect in line 22 and not in line 14
I tried the rect in line 22 prior to putting it before. None of them work.
line 22 is correct
this must be inside the inner loop btw.
x+=40;
<<<<[EDITED]
also you need to reset it when going to the next line <<<<
I got it working now
Thank you! works now! Thanks everyone for input. Here's the solution for anyone wondering:
I like your code.
Two remarks:
You can have line breaks in line 7 in YOUR code (
{0, 1, 0, 1, 1}, {1, 0, 1, 0}, .....
) andmove line 14
x+=60;
after line 22 (because now everything is moved 60 to the right)well done!
Woah cool code @GoToLoop. We haven't learned some techniques you're using so so I'm not allowed to use it :( but I might be learning those ways later in the semester!
HEH, HEH! :ar!