-1

I'm preparing for exams and there's this question I can't find answer to. Read bunch of articles, closest I found was

Arrays in C# come in three flavors: single-dimensional, multidimensional rectangular arrays (like the C++ multidimensional arrays), and jagged arrays (arrays of arrays).

So this suggests that there's no jagged arrays in C++, but it exists in Java. Another thing is that just C# can have non-zero array lower-bound(like a[-1,3] or a[4,9]. Would it be considered different array structure?

Erndob
  • 2,512
  • 20
  • 32
  • C and C++ also have jagged arrays. You need to create it specifically. – haccks Jun 04 '15 at 10:44
  • Why the downvoting? This isn't a very bad question... He could have done some research... but I don't think it is *that* bad. – xanatos Jun 04 '15 at 10:49
  • @haccks, it doesn't matter. I need an answer to what array structure exists just in C#, if jagged array exists in Java I don't care that it exists in C++ too, or vice versa. – Erndob Jun 04 '15 at 11:02

1 Answers1

5

C++

  • So this suggests that there's no jagged arrays in C++

By the same reading, the block of text suggests that C++ doesn't have single dimensional arrays. This is clearly absurd!

C++ has both... You clearly can make a int**, that is a pointer to a pointer (so an "array" of pointers, so an "array" of "arrays"), like in C# you can have a int[][], that is an array of int[]. For C++ see various examples here. Note that this syntax is more C than C++... In C++ you should use std::array, like here.

  • Another thing is that just C# can have non-zero array lower-bound(like a[-1,3] or a[4,9])

This doesn't exist in C++... They are internally implemented in C# by the same code that implements multi dimensional arrays, and exist for historical reasons (pseudo-compatibility with old versions of VB)

Java

Java doesn't have multi-dimensional arrays (see here). It does have jagged arrays, with a trick: if you want you can initialize a jagged array that has all the elements of the same size in a single command or if they have different sizes/some of them can be null, you can initialize them manually.

int[][] num = new int[4][2];

vs

int[][] num = new int[4][];
num[0] = new int[1];
num[1] = new int[2];
num[2] = new int[3];
num[3] = new int[4];

So in the end

                            C#    Java  C++   
single-dimensional array    x     x     x
multi-dimensional array     x           x
si.di. non-zero based array x
mu.di. non-zero based array x
jagged array                x     x     x
Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Yeah, so that's the problem, I've read a bunch of what is the same, I need an answer to what array structure exists just in C#. So that non-zero lower-bound would be considered good answer? To me it felt just like some minor difference instead of entirely different array structure. – Erndob Jun 04 '15 at 10:57
  • @Erndob non-zero lower bound are a complex beast... You can't create them directly in C#, you have to use `Array.CreateInstance` to create them... and their type is the same as the type of other multidimensional array (with the exception of single-dimensional non-zero lower-bound, that uses a special array type). So they are of the same "family" of multi-dimensional array (because they use the same CLR internal classes). – xanatos Jun 04 '15 at 11:01
  • @Erndob In the end in .NET there are two types of array: "simple arrays": single-dimensional arrays that start with 0, "slow complex arrays": multidimensional or non-0 indexed or both. Jagged arrays are an overstructure that you can put on both of them, because a jagged array is an array of arrays. – xanatos Jun 04 '15 at 11:03
  • I thought so, so what array structure is in c# but not in java/c++/c ? Because I've read a bunch, and people in this thread answer about things that are the same, not different. Or its just bad question? – Erndob Jun 04 '15 at 11:05
  • @Erndob See the final table – xanatos Jun 04 '15 at 11:32
  • So it s non-zero array, thanks. – Erndob Jun 04 '15 at 11:36