Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (harsha19111999):

Can anyone help me with Arrays in JAVA?

OpenStudy (harsha19111999):

@LolGoCryAboutIt

OpenStudy (anonymous):

What did you need help with exactly?

OpenStudy (anonymous):

I.E. Do you have a sample problem, so something specifically that you're confused about?

OpenStudy (harsha19111999):

I want to know how to create 2-D arrays

OpenStudy (anonymous):

Okay, let's start with that.

OpenStudy (anonymous):

Do you conceptually understand what a 2D array is?

OpenStudy (harsha19111999):

Yes a array consisting of rows and columns

OpenStudy (anonymous):

Exactly. Now, do you know how to create a one-dimensional array in Java?

OpenStudy (harsha19111999):

Yes

OpenStudy (anonymous):

Okay, show me how you would create a 1D array consisting of 5 rows.

OpenStudy (harsha19111999):

For 5 rows, I can use like: type var[]; var = new type[5]; or for an auto array, I can use type var[] = {.....};

OpenStudy (anonymous):

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];

OpenStudy (harsha19111999):

Okay..

OpenStudy (anonymous):

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]

OpenStudy (anonymous):

So in essence, each row (ex. var[2]) is an array of items, which we call a column.

OpenStudy (anonymous):

Do you know how to set up a loop for a 1D array to initialize values?

OpenStudy (harsha19111999):

No

OpenStudy (harsha19111999):

Could you explain?

OpenStudy (anonymous):

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; ?

OpenStudy (harsha19111999):

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

OpenStudy (anonymous):

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;

OpenStudy (anonymous):

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]

OpenStudy (anonymous):

Let me know if you are confused or need any further explanation

OpenStudy (harsha19111999):

Yeah. I have declared the numbers as you said

OpenStudy (anonymous):

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

OpenStudy (harsha19111999):

And I have added this System.out.println(var[0][1]); at the last to print out 4

OpenStudy (anonymous):

Yup! That should work!

OpenStudy (harsha19111999):

Yes. It did work. Is there a Auto-array for 2-D also?

OpenStudy (anonymous):

There is - but I have never done so. Let me look at how it's done.

OpenStudy (harsha19111999):

Okay. Sure

OpenStudy (anonymous):

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}};

OpenStudy (anonymous):

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;

OpenStudy (harsha19111999):

Oh okay. Thanks a ton. Are you available tomorrow?

OpenStudy (anonymous):

I'm on randomly. Just tag my name, if I'm on I'll respond!

OpenStudy (harsha19111999):

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

OpenStudy (anonymous):

No problem, have a good night!

OpenStudy (harsha19111999):

Thank you :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!