• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multidimensional array

 
Bhadresh Patel
Greenhorn
Posts: 4
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a one program but i dont know the answer so please help me
Question :
you have array like this
_______________
0,1,2,3,4,5,6,7,8
_______________
and you have to put in under

0|1|2|
3|4|5|
6|7|8|

i want that answer in cube with exact position like i want..?
please give me the correct answer. ?
thank you
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read HowToAskQuestionsOnJavaRanch, particularly the ShowSomeEffort and NotACodeMill sections.
 
Bhadresh Patel
Greenhorn
Posts: 4
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please sir help me
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhradesh,

Welcome to the Ranch

Show us what have you tried. You can see a tutorial on arrays here
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhadresh Patel wrote:Please sir help me

Did you read those links I posted ? You seem to have missed one important part
We ... make a point not to give out working code to someone who wants to turn it in as a homework assignment or dump it into their project without knowing what it does. We would much rather see you post your own code, working or not, and give you tips to help you get it working
 
Bhadresh Patel
Greenhorn
Posts: 4
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is the way i do that.. but i want values from user........
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhadresh Patel wrote:this is the way i do that.. but i want values from user........

Use a Scanner to get input from the user.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bhadresh Patel!!

I took your code, and put it into 'code tags'. see how much better it looks? If you did this, it makes it easier for other folks to read, and thus they are more likely to help you. Next time you make a post, look for the little button above where you enter your text. Hi-light all of your code and click the button. You should see something like this before and after your code:

[ code=java]
[ /code]

(Note: the real ones don't have a space after the opening brackets).
 
Bhadresh Patel
Greenhorn
Posts: 4
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will remember next time....
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Break down your problem into smaller components. Then instead of staring at one big problem and wondering where to start, you have a set of small problems. If you still don't know where to start, try breaking the steps down into still smaller problems.

Here's what the first iteration of breaking down the problem might look like, based on the (meager) information you've given us:
1. Obtain user input
2. Store that input in a flat array
3. Transform the flat array
4. Display the result of the transformation

Each of these tasks can, itself, be broken down into smaller bits. Keep breaking down until the way to move forward is self-evident. And it's important to do this away from the computer, with pencil and paper. Pretend you have to prepare a set of instructions for another person to follow. They know nothing whatsoever about what your program should do. So you must explain every step, with complete clarity. Tell me how I would perform this task without a computer.

Once you have a set of instructions a human could carry out, then you are ready to write code. Note that using this approach, you can implement any discrete step of the program without having to rely on any of the others being implemented. If we don't know (yet) how to obtain user input, we can nonetheless work on transforming the array, and vice versa.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised Campbell hasn't pointed this out yet, but there java doesn't have muti-dimensional arrays. It only has 1-dimensional array, however what those can hold includes...arrays.
 
Rares Boston
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So

Isn't a multidimensional array? Wouldn't an array holding arrays be multidimensional?
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rares Boston wrote:Isn't a multidimensional array. Wouldn't an array holding arrays be multidimensional.

I presume this is a question, and the answer is: no; although it may look like one.

In C (which does have multi-dimensional arrays), an int[5][5] is, quite literally, a matrix mapped into memory. When you want to access, say element [2][3], the language converts that to an offset of (2*5) + 3 from the start of the matrix and retrieves the integer at that location.

In Java, on the other hand, an int[5][5] is a one-dimensional array of 5 int[]'s, each of which contains 5 ints, and which may be scattered all over creation, so when retrieving element [2][3], the language converts that to instructions to retrieve the 4th element (indexes are 0-based, remember) of the int[] referenced in the 3rd entry of the first array.

HIH

Winston
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I'm surprised Campbell hasn't pointed this out yet, but there java doesn't have muti-dimensional arrays. It only has 1-dimensional array, however what those can hold includes...arrays.


What do you mean java doesn't have multidimensional arrays?

Yes it does.

int[][] 2DArray = new int[5][5];

 
Rares Boston
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Adcock wrote:
fred rosenberger wrote:I'm surprised Campbell hasn't pointed this out yet, but there java doesn't have muti-dimensional arrays. It only has 1-dimensional array, however what those can hold includes...arrays.


What do you mean java doesn't have multidimensional arrays?

Yes it does.

int[][] 2DArray = new int[5][5];



Look at the post above yours, I asked the same question.

Winston Gutkowski wrote:
Rares Boston wrote:Isn't a multidimensional array. Wouldn't an array holding arrays be multidimensional.

I presume this is a question, and the answer is: no; although it may look like one.

In C (which does have multi-dimensional arrays), an int[5][5] is, quite literally, a matrix mapped into memory. When you want to access, say element [2][3], the language converts that to an offset of (2*5) + 3 from the start of the matrix and retrieves the integer at that location.

In Java, on the other hand, an int[5][5] is a one-dimensional array of 5 int[]'s, each of which contains 5 ints, and which may be scattered all over creation, so when retrieving element [2][3], the language converts that to instructions to retrieve the 4th element (indexes are 0-based, remember) of the int[] referenced in the 3rd entry of the first array.

HIH

Winston

Thats the answer.
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rares Boston wrote:
Paul Adcock wrote:
fred rosenberger wrote:I'm surprised Campbell hasn't pointed this out yet, but there java doesn't have muti-dimensional arrays. It only has 1-dimensional array, however what those can hold includes...arrays.


What do you mean java doesn't have multidimensional arrays?

Yes it does.

int[][] 2DArray = new int[5][5];



Look at the post above yours, I asked the same question.



Also, the example isn't valid, as Java doesn't allow a variable to be named "2DArray".

Henry
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, forgot about that. Was just making a quick example.

Anyway, so how would it access it compared to like in C/C++?

For the example you provided above.

What does it map to? How can an int store 5 ints?

 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Adcock wrote:What does it map to? How can an int store 5 ints?

It doesn't. An int[] can certainly store 5 integers though; and, as I explained above, an int[][] is a one-dimensional array of int[]'s.

Furthermore, an int[][][] is a one-dimensional array of int[][]'s; and so on, and so on...

Winston
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Adcock wrote:Yeah, forgot about that. Was just making a quick example.

Anyway, so how would it access it compared to like in C/C++?

For the example you provided above.

What does it map to? How can an int store 5 ints?



I actually somewhat agree with you. With both Java and C, the code that access the elements look similar -- and if something looks like a duck...


Anyway. some differences.... In C, a one dimensional int array of size 25 should have the same size as a two dimensional int array of size 5 by 5. There is a formula, give the indexes, to calculate the location of each element. In Java, a "2D" array is actually an array of arrays. You have an array of array references that points to 5 other arrays somewhere on the heap. And you have five int arrays.

If all you want to do is access specific elements in the "2D" array, the code should be similar. However, with Java, you can access a particular int array, or even change array elements of the outer array. Also, "2D" arrays don't have to be rectangular shaped. The outer array may be size 5, while the 5 inner arrays may be all different sizes.

Henry
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:I actually somewhat agree with you. With both Java and C, the code that access the elements look similar -- and if something looks like a duck...

Anyway. some differences.... In C, a one dimensional int array of size 25 should have the same size as a two dimensional int array of size 5 by 5. There is a formula, give the indexes, to calculate the location of each element.

Actually, you have to be a bit careful: In C, you can define an int[][], a *int[], and a **int, which are somewhat interchangeable when defined as parameters, even though the underlying memory mapping could be totally different.

In Java, there's no confusion, providing you remember that each additional set of []'s on the left defines an array of the ones on the right.

Winston
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Henry Wong wrote:I actually somewhat agree with you. With both Java and C, the code that access the elements look similar -- and if something looks like a duck...

Anyway. some differences.... In C, a one dimensional int array of size 25 should have the same size as a two dimensional int array of size 5 by 5. There is a formula, give the indexes, to calculate the location of each element.

Actually, you have to be a bit careful: In C, you can define an int[][], a *int[], and a **int, which are somewhat interchangeable when defined as parameters, even though the underlying memory mapping could be totally different.

In Java, there's no confusion, providing you remember that each additional set of []'s on the left defines an array of the ones on the right.

Winston


Isn't *int[] an array of pointers?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I'm surprised Campbell hasn't pointed this out yet, . . .
That is because he was away at the time and didn’t see the post.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Adcock wrote:Isn't *int[] an array of pointers?

Yes, but in C it can be used interchangeably with **int in some cases (eg, in main() method declarations; except it's **char of course).

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic