开发者

2D Array - C vs Java

开发者 https://www.devze.com 2023-04-10 09:23 出处:网络
What is the difference between a Dynamic 2D Array in C (int *开发者_如何学运维*arr) and A 2D Array in Java (int [][]arr) ?

What is the difference between a Dynamic 2D Array in C (int *开发者_如何学运维*arr) and A 2D Array in Java (int [][]arr) ?

Is a 2D Array in C/C++ similar to a 2D Array in Java?


The answer will change according to part you want to compare.

In Java, arrays are objects that have methods and attributes, however in C/C++ they are not objects.

Both of them similar in a way that array dimension can be different. I mean arr[0] can be 3-dimensional, arr[1] 5-dimensional, etc.

Java is checking array bounds therefore will throw exceptions if you try to reach an index outside of the array boundaries, however in C/C++ no exception will be thrown however, you might end up with an "segmentation fault".


Very much similar. Of course, arrays in Java are full-fledged objects instead of "bare" sequences of the constituent types, but multi-dimension arrays are done effectively the same way, with an array of pointers to arrays.


Yes, they are quite similar. The benefit of the java arrays is that you cannot access a cell that doesn't exist (you get an ArrayOutOfBoundsException).

I think the important bit is that both are references to an array, so there's no duplication/cloning of the arrays when you pass them to a method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号