aspose file tools*
The moose likes Java in General and the fly likes multidimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "multidimensional array" Watch "multidimensional array" New topic
Author

multidimensional array

Bhadresh Patel
Greenhorn

Joined: Jun 22, 2012
Posts: 4

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

Joined: May 30, 2012
Posts: 679
You need to read HowToAskQuestionsOnJavaRanch, particularly the ShowSomeEffort and NotACodeMill sections.
Bhadresh Patel
Greenhorn

Joined: Jun 22, 2012
Posts: 4

Please sir help me
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Bhradesh,

Welcome to the Ranch

Show us what have you tried. You can see a tutorial on arrays here
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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

Joined: Jun 22, 2012
Posts: 4


this is the way i do that.. but i want values from user........
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 02, 2003
Posts: 11494
    
  16

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).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bhadresh Patel
Greenhorn

Joined: Jun 22, 2012
Posts: 4

i will remember next time....
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Oct 02, 2003
Posts: 11494
    
  16

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

Joined: Oct 30, 2011
Posts: 6
So

Isn't a multidimensional array? Wouldn't an array holding arrays be multidimensional?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8398
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

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


Repeal Obamacare.
http://www.dontfundobamacare.com/
http://liberty-amendments.freeforums.net/
Rares Boston
Greenhorn

Joined: Oct 30, 2011
Posts: 6
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
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Adcock
Ranch Hand

Joined: Jan 22, 2011
Posts: 48

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

Joined: Mar 17, 2011
Posts: 8398
    
  23

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
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

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

Joined: Mar 17, 2011
Posts: 8398
    
  23

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

Joined: Jan 22, 2011
Posts: 48

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

Joined: Oct 13, 2005
Posts: 40027
    
  28
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

Joined: Mar 17, 2011
Posts: 8398
    
  23

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multidimensional array