0

I have a 2D array in Java that is

 [[1 1 1 1 1] [2 2 2 2 2] [3 3 3 3 3] [4 4 4 4 4]]

And I was wondering how this would appear if drawn out on paper, i.e. what does each individual array translate to.

So would the above array appear as, A:

 1 2 3 4 
 1 2 3 4 
 1 2 3 4 
 1 2 3 4
 1 2 3 4 

or, B:

1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 

I know this may seem a basic question but I can't find an answer and it is obviously fundamental to my programme.

1
  • Neither, and both. And however you utilise it really. Commented Jun 12, 2014 at 9:55

5 Answers 5

6

C:

1 1 1 1 1   2 2 2 2 2   3 3 3 3 3   4 4 4 4 4

Whether they are rows or columns is meaningless and arbitrary in this case, and depends entirely on how you interpret the data. It is simply an array of arrays, and whether you decide it's column-first, or row-first is entirely up to you. Just make sure you always do it the same way.

If you want a meaningful row/column relationship, you should wrap it in a Table class, or use one that someone else has made for you (Guava comes to mind).

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

2 Comments

I am trying to replicate a multidimensional array I have in MATLAB which is version A, if I were to use the 'array of array' within JAVA would it be feasible to complete dot products and other further calculations? The array is MATLAB is actually 3 dimensional
MATLAB has a Java API IIRC. Is there some reason you're not using that? But sure. It's perfectly feasible, albeit not entirely practical. There are however quite a few advanced mathematical libraries for Java that you could use to make this much easier.
3

This depends on how you perceive and use it.

Comments

0

It would look like B .

int[2][3] means that there are 2 arrays of length 3.

so it would be like

array1 : x x x
array2 : x x x

This is how they are located in memory, you can print them in various ways of course.

Comments

0

The answer is B.

Remember that there is no multidimensional array in Java, it is actually an array of array , just like your array :

[
   [1 1 1 1 1],
   [2 2 2 2 2],
   [3 3 3 3 3],
   [4 4 4 4 4],
]

Comments

0

B, check it by code below

int[][] a = new int[][]{{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3},{4,4,4,4,4}};
        for(int i=0;i<4;i++){
            for(int j=0;j<5;j++){
                System.out.println(a[i][j]);
            }   
        }

3 Comments

what if you interchange your for loops?
Aren't your for loops deciding which was the array is presented ?
But the structure is already decided. If not like B. This loop will cause an error. Maybe my answer is just not so clear XD. I m a beginer too :]

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.