Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Two Dimentional Array

Venkat Paspula
Greenhorn
Posts: 11
Hello all,
I need a small help in 2 dimentional array.
I have array like this
int [][] student = new int [][] {{1,2,3,4}, {100,200,300,400}};
I want to print something like this

Student : 1 Marks are: 100 200 300 400
Student : 2 Marks are: 100 200 300 400
Student : 3 Marks are: 100 200 300 400
Student : 4 Marks are: 100 200 300 400
[ April 09, 2006: Message edited by: Venkataramana Pasula ]

Svend Rost
Ranch Hand
Posts: 904
should get that output.

I'll give you a pointer though: loop.

I'll look back later for your solution, and provide you with some
more help if you need it.

/Svend Rost

Venkat Paspula
Greenhorn
Posts: 11
Hello Svend

Actually I posted the code.But I messed up when I edited the post.Any ways here is the code

Tony Morris
Ranch Hand
Posts: 1608
It might help to think of your "two-dimensional array" instead as an array of arrays ...since Java does not have multi-dimensional arrays.

Once you understand that you have an array, and you know how to iterate an array (easy right?), then iterating an array that is merely an element of another array is easy.

Rusty Shackleford
Ranch Hand
Posts: 490
That is true, but I think it complicated matters. You can draw a java array of array as a table on paper and the iterations in java code work just fine as a 2d array.

Abstracting it to a table or cube("4d+" arrays are truly heinous) makes it much easier to learn them in my opinion. The classic assignments dealing with 2d arrays, mazes, are much, much easier to grasp if you think of them as a table.

Just my opinion.

Tony Morris
Ranch Hand
Posts: 1608
Originally posted by Rusty Shackleford:
That is true, but I think it complicated matters.

My experience suggests otherwise. How much easier is it to think of a single array of elements of arbitrary type, than it is to think about multi-dimensional arrays? I postulate, much easier. The only hard part, like many things, is abandoning preconceived falsifications. Once this is achieved, the clarity is profoundly obvious.

To prevent ill-conceived ideas, don't talk about multi-dimensional arrays at all - since they are out of context. Unfortunately, Sun (and other) documentation erroneously refers to an imaginary construct, therefore, urge those who are learning to constantly question the authenticity of their perceived authority.

Rusty Shackleford
Ranch Hand
Posts: 490
Are you saying you know Java better then its creators?

Abstracted it is a table. Java (and most other languages) quite often abstracts what is actually going on, down to something simpler.

Whether or not it is truly an n-dimensional array, it acts exactly like it, and is easier for people to think about it in those terms, with no negative side-effects.

Tony Morris
Ranch Hand
Posts: 1608
Originally posted by Rusty Shackleford:
Are you saying you know Java better then its creators?

Abstracted it is a table. Java (and most other languages) quite often abstracts what is actually going on, down to something simpler.

Whether or not it is truly an n-dimensional array, it acts exactly like it, and is easier for people to think about it in those terms, with no negative side-effects.

Abstracted, it is not a table (unless you understand the severe limitations). I could say that "abstracted, the sky is a typewriter" as long as the one I was communicating with understood the limitations in my assimilation, albeit perhaps being more extreme in this case. I make no such assumption of anyone - not in a beginner forum - not on any publicly-available forum where the average audience may not have a clue what you're on about.

A table does not act exactly like an array of arrays, with many adverse side-effects (search this forum if you like).

I don't claim to know Java better than anyone; in fact, every claim of that nature that I have ever seen can be discredited in under 10 seconds (just what does it mean to "know Java" anyway?).

I do, however, have at least a basic understanding of programming language theory and the related mathematics; something that Java language designers were (and unfortunately, still are) severely lacking. What's it got to do with anything anyway?

The distinction between an array and a multi-dimensional array (you have referred to it as a "table" (see COBOL)) have been known for quite some time - is some research in order?

Rusty Shackleford
Ranch Hand
Posts: 490
Originally posted by Tony Morris:

Abstracted, it is not a table (unless you understand the severe limitations). I could say that "abstracted, the sky is a typewriter" as long as the one I was communicating with understood the limitations in my assimilation, albeit perhaps being more extreme in this case. I make no such assumption of anyone - not in a beginner forum - not on any publicly-available forum where the average audience may not have a clue what you're on about.

A table does not act exactly like an array of arrays, with many adverse side-effects (search this forum if you like).

I don't claim to know Java better than anyone; in fact, every claim of that nature that I have ever seen can be discredited in under 10 seconds (just what does it mean to "know Java" anyway?).

I do, however, have at least a basic understanding of programming language theory and the related mathematics; something that Java language designers were (and unfortunately, still are) severely lacking. What's it got to do with anything anyway?

The distinction between an array and a multi-dimensional array (you have referred to it as a "table" (see COBOL)) have been known for quite some time - is some research in order?

I will follow the rules of the board, since I believe it is what makes these boards the best around(ever been to the forums at java.sun.com? intimidating place). So all I will say about your statement about abstracting the sky into a typewriter, is that it is the most pointless statement I have ever read and has no basis in fact. I would however, enjoy an attempt at it.

The reason I asked about if you think you know more then the designers is that you are often going against conventional wisdom just for the sake of it.

Draw a table and put whatever you like in each element. Move around however you like and write down the steps(ie 3 elements to the left, 2 elements down). Now write the java code for the same steps, it is EXACTLY the same. You are exactly where you need to be. Hence thinking of a table is a very legitimate abstraction and has no ill effects. All you did was say don't think about it as a n-dimensional array and linked to yourself as proof.

The whole point of java is hiding details from the programmer, memory management is completely abstracted away from the programmer, as is IO, sockets, data structures, ect. Abstraction is the point. If you like you can think of java primitives and immutable objects as being passed by value, and mutable objects as pass by reference, that is not what is going on in memory but that is its behavior.

I agree with you 100% about knowing specifics, that is my major problem with java. So many people just learn java, take those sily exams and call themselves a programmer. That is as far from the truth as you can get. Doing that makes you a java tools end-user. Which is the main reason I am a computer science major.

Saying it works like a table in a beginners forum is completely appropriate. Later on, learning what is happening behind the scenes is more productive. That is the very reason alot of universities are now teaching java in their introdutory courses. You do not need to learn what is happening underneath to be effective. Unlike C and of course whatever variety of assembly you prefer.

Tony Morris
Ranch Hand
Posts: 1608

The reason I asked about if you think you know more then the designers is that you are often going against conventional wisdom just for the sake of it.

I strongly object to this claim. I approach truth within my capability. I ignore "conventional wisdom" since it has no basis in fact (the contrary if anything ala history). In any case, the difference between an array and a table is "conventional wisdom" that happens to align with what I also understand to be the case (and you do not?). Therefore, it is reasonable to suggest that it is you who are violating "conventional wisdom"... not that this in any way implicates truth or otherwise.

To blur the distinction on a beginner forum is completely inappropriate. If you and I were to discuss these concepts, and we had a mutual understanding of the definition of our terminology, then and only then, is it appropriate. Assuming a "beginner" does not have this common premise, it is paramount that clarity is attempted to the best of one's ability.

Am I just making it all up? Like I said, I believe this as a result of my experience, particularly that through teaching students who are trying to learn without any preconception (e.g. at tertiary institutions). I make greater effort to ensure a common understanding in these cases. I make smaller efforts to reason with those who have internalised the nonsense (and likewise, sometimes I discover that I have internalised "guff").

That it is common to blur reality for the sake of simplicity is exactly the reason that most Java programmers (that I continually encounter) do not have a sound understanding of the theory on which the topics at hand rest. It is the reason that vulnerable minds are being misled. I strongly object to misleading the innocent and I will side with their cause (to seek an understanding of truth without hidden agenda of those in which they trust) for the foreseeable future. I'd expect the same from others in contexts where I may be considered relatively "innocent".

Paul Clapham
Sheriff
Posts: 21107
32
Well, if you start with the concept of an array of items of a single type, then there are two ways to abstract that concept to a 2-dimensional array. One is to simply allow the elements of an array to be other arrays; this automatically provides the abstraction to 3 dimensions and higher. This is the approach used by the Java designers.

The other way to abstract the concept is to consider that an array has a single dimension with a length, and to abstract that by saying that a 2-dimensional array has two dimensions, each with a length. The abstraction to 3 dimensions and higher is obvious. This is the approach used in Fortran.

The second (Fortran) version does indeed look like a table: a rectangular grid with R rows and C columns. But the first (Java) version need not look like a table, because for example you could have an array with 3 elements: the first is an array of 2 integers, the second is an array of 12 integers, and the third is an array of 5 integers.

So, unfortunately, a multidimensional array in Java is not an abstraction of a table, but something more complicated than that. Whether beginners should have that distinction made clear right away is a question for the psychologists rather than the mathematicians, perhaps.

Tony Morris
Ranch Hand
Posts: 1608

Whether beginners should have that distinction made clear right away is a question for the psychologists rather than the mathematicians, perhaps.

I agree.
All I can do is draw a conclusion based on my (perhaps inept?) experiences with teaching, dealing with hitherto "beginners", etc. I'd love to hear of refutations or greater understandings than that of myself.

I began studying psychology a few years back, but decided to give it a miss