1

How do I add 1 position and 1 position only in a 2d array. I tried adding the array value like a 1d array but multiple element were added. Can anyone help me?

This is a 2d array:

1

Is it possible to do this:

2

2
  • Unclear question, please revise. Commented Aug 10, 2014 at 1:31
  • It seems that OP tries to resolve the same problem than in how-to-turn-list-into-a-square-list but with array. Commented Aug 10, 2014 at 1:47

1 Answer 1

3

It's not possible. Every language I know of requires matrices to be "rectangular." I would recommend either using a 2X2 matrix plus a variable, or a column or row vector of length five. You can also create a 3X2 or 2X3 matrix and just choose to leave one element as NaN or 0 etc. I may be able to answer your question better if you leave a comment telling me the reasoning behind wanting a non rectangular matrix.

EDIT: I was wrong you can create non rectangular matrices in Java link.

Sign up to request clarification or add additional context in comments.

4 Comments

Java is one exception to your "rectangular matrices" statement. I believe that there are other languages that allow this kind of feature, too.
It depend the data structure used for storage the matrix, for example std::vector<std::vector<int>> would let you do the behavior asked if you want, i understand that would not be a matrix anymore, but let you do it.
These folks are right! You can create non rectangular arrays in Java (also C#). Here is a link showing you how link. I will edit my post to reflect this change. Thank you @Streppel!
Now that I've read @NetVipeC's comment, I realized that my comment kind of twisted the concept of a matrix. If I could edit it back again, I'd written array of arrays, which is what they are actually called, and not a matrix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.