Can anyone help me with Arrays in JAVA?
@LolGoCryAboutIt
What did you need help with exactly?
I.E. Do you have a sample problem, so something specifically that you're confused about?
I want to know how to create 2-D arrays
Okay, let's start with that.
Do you conceptually understand what a 2D array is?
Yes a array consisting of rows and columns
Exactly. Now, do you know how to create a one-dimensional array in Java?
Yes
Okay, show me how you would create a 1D array consisting of 5 rows.
For 5 rows, I can use like: type var[]; var = new type[5]; or for an auto array, I can use type var[] = {.....};
Perfect! Okay, now for a multidimensional array, all that means is, each index in your var array will also be an array. So for example, let me declare a 2D array like so: int[ ][ ] var = new int[5][3];
Okay..
Now, within var you have row indexes 0-4. You can think of a multidimensional array like so: var[0] consists of items var[0][0], var[0][1], var[0][2] var[1] consists of items var[1][0], var[1][1], var[1][2] var[2] consists of items var[2][0], var[2][1], var[2][2] var[3] consists of items var[3][0], var[3][1], var[3][2] var[4] consists of items var[4][0], var[4][1], var[4][2]
So in essence, each row (ex. var[2]) is an array of items, which we call a column.
Do you know how to set up a loop for a 1D array to initialize values?
No
Could you explain?
I can - but first let me ask - do you know how to work with loops (while loops, for loops, etc.)? Have you been using loops to set up your array, or, are you setting your values like so: var[0] = 5; var[1] = 12; ?
Yeah I know how to set loops. But I don't know how to set loops for arrays. I have been using the 1-D arrays like the one you have said above
If you want to set your values, you just do the following, just like a 1D array: (FYI I'm just picking random integers to set the array indexes to) var[0][0] = 12; var[0][1] = 4; var[0][2] = 17; var[1][0] = 81; var[1][1] = 0; var[1][2] = 46; var[2][0] = 65; var[2][1] = 82; var[2][2] = 19; var[3][0] = 90; var[3][1] = 11; var[3][2] = 7; var[4][0] = 30; var[4][1] = 44; var[4][2] = 64;
This is similar to: var[0] = 7; var[1] = 80; var[2] = 5; var[3] = 100; ...etc. Except that now, for every "row" you have a certain number of columns to set. i.e. var[0] has 3 columns. Hence, we need to set var[0][0], var[0][1], and var[0][2]
Let me know if you are confused or need any further explanation
Yeah. I have declared the numbers as you said
That's it! Your 2D array is all set to be used. Just use the 2D array like so: someNumber = int[1][2]; //someNumber now equals 46
And I have added this System.out.println(var[0][1]); at the last to print out 4
Yup! That should work!
Yes. It did work. Is there a Auto-array for 2-D also?
There is - but I have never done so. Let me look at how it's done.
Okay. Sure
Okay, so I wrote a quick program to confirm, and you can set it up like this: int var[][] = {{1,2,3},{4,5,6}};
That's the equivilent to: var[0][0] = 1; var[0][1] = 2; var[0][2] = 3; var[1][0] = 4; var[1][1] = 5; var[1][2] = 6;
Oh okay. Thanks a ton. Are you available tomorrow?
I'm on randomly. Just tag my name, if I'm on I'll respond!
Okay. I live in India its about 11:36 PM. Time to sleep. I'll text you tomorrow. Bye. And thanks again for the help
No problem, have a good night!
Thank you :)
Join our real-time social learning platform and learn together with your friends!