How do I create a jagged 2d array in Java? - java

Our homework assignment asks us to use a jagged array to store the values of a two dimensional boolean matrix. Is there a built in java class for the jagged array or am I going to have to manually create it with an Array of ArrayLists?

In Java, a 2D array is an array of 1D array objects. Each 1D array can have different length, which means that you get jagged arrays out of the box.
For example, the following is perfectly valid Java, and prints out 3 5 3 4:
int x[][] = {{0,1,2,3,4},{0,1,2},{0,1,2,3}};
System.out.println(x.length);
System.out.println(x[0].length);
System.out.println(x[1].length);
System.out.println(x[2].length);

It actually sounds like you might want a sparse matrix implementation. You can get much better performance out of it if you are having to modify the matrix. Array copy operations are pretty expensive. Sparse matrices / arrays in Java

Related

Creating a 3D array in Java

I'm trying to implement a 3D array in Java but I've a problem, my problem is that I don't know one of the lengths of 3D array size, it means that third length of my 3D is variable and it depends on
the input size. In other words my 3D array called
int arcbits[64][1][length(input)];
first two sizes are fixed, it's always [64][1] and just the third length is variable.
length(input) is always positive integer greater zero.
Input is like this form = {1,0,1,1}, so in this case for instance the arcbits size is:
int arcbits[64][1][4];
How do I implement that in Java? My problem is that there's a variable length which for instance in c++ or c we do dynamic allocation ...because we don't know the size of the array. So do I do 3D array in Java with implicitly variable size?!
I'm stuck on this about two days and I didn't succeed to implement 3D array in Java, this is the first time I counter this, any suggestion to help me out?
int[][][] arcbits = new int[64][1][length(input)];
See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
see here:
Sum in Java (built in method)'s Implementation?
I already declared 3d array in java, I guess you look on the same concept.

In Android or Java, is there a way to resize a two-dimensional matrix

Let's say I have a 100 by 120 matrix, but I want to have a size of 120 by 120 or 100 by 100, is there any way I can transform this matrix?
In Python, I use opencv.resize, because this function can handle arrays directly, but in Android and Java, it seems that you can't handle arrays directly. Cvresize can handle cVARr data, is there any way to convert a two-dimensional array to Cvarr?
Or some other way?
In java, if you want to work with static arrays (fixed size) which normal arrays are, you will need to create a new 2 dimensional array of 120x120 size for example because you can't change the size of a static array. All its elements will be initialized to 0 by default, finally you just need to copy your 100x120 matrix in that array leaving all the untouched elements to 0.
If you are working with dynamic arrays (variable size) which is a variable-size list data structure like ArrayList, you dont need to create a new array beacuse you can manipulate the size of this ArrayList. So you just increase its size or decrease It depending on what dimensions you want.

Why does the multidimensional array syntax work

When we create a 2d array such as int[][] a = new int[2][3] why is the resulting 2d array consist of a two-element array that contains three-element int arrays instead of the other way around. The reason why I'm confused is that when we make an array we do datatype[], so when we do int[2][3] why don't we put three int[2] arrays into an array with three spots (from the [3]).
The way it's implemented in Java is more logical. Consider the array element access expression: a[x][y]. Currently, it could be nicely decomposed to (a[x])[y] which means "we get an x-th element of a, then we get a y-th element of the result". So imagine if new int[2][3] produced an array of three elements, each is a two-element array. Then the x should be in range 0..2 and y should be in range 0..1 which is the opposite of the dimension order used at the array creation point. That would be absolutely confusing.
I guess you have a point with your logic. Eventhough you could also argument, writing int[2][3] means "first index can have 2 different values, second 3", what leads to the same as how it really works.
In the end, this is just a matter of specification and compilerbuilding. And since it is specified this way and not that way, it is implemented and works this way.

Pass matlab size matrix to java

How can I pass a MATLAB result s like shown below to a Java method JSize()
s = size(oImage)
s =
91 121 3
First off, you would need to know how many dimensions your array has. Because this looks like an image, I'm going to assume that you'll expect a 3D array.
Because Java considers multidimensional arrays as an array of arrays, it isn't as dynamic as MATLAB where you can simply figure out how many dimensions there are by just checking the length of the size vector.
Assuming that your matrix is not jagged, you can determine how many rows you have by:
int rows = oImage.length;
If you want to determine how many columns there are, you can use any of the rows in your matrix and obtain its length:
int cols = oImage[0].length;
If you want to see how many elements there are in each 2D location in your matrix, you would just access any column in any row you specify and get its length. In our case, let's stick with oImage[0]:
int dim = oImage[0][0].length;
Therefore, you could write a Java method that could return this as an array of elements similar to size in MATLAB:
public int[] JSize(int[][][] oImage) {
return new int[] {oImage.length, oImage[0].length, oImage[0][0].length};
}
Remember, Java has the capacity of declaring jagged multi-dimensional arrays. This means that each row in your 2D matrix does not necessarily have to have the same number of elements like what you would see in a matrix. If you have a multi-dimensional array in Java that follows the above model, then the above code wlll work.

Speed of iterating through 1-dimensional vs 2-dimensional arrays

I was reading a post on how iterating through a 2-dimesional array horizontally is faster than vertically because of the way the data is stored(See:Fastest way to loop through a 2d array?). That made sense when I read the answer but it got me wondering what the difference was between 2 and 1 dimensional arrays. Is there any speed difference in iterating 1-dimension vs 2-dimension arrays with the same number of cells?
On Java, there are many more factors and more overhead with arrays. As arrays are objects, int[][] is an array of array objects of ints. This may make horizontal iteration faster than vertical if hotspot optimizes or caches the array access.
For one vs two dimensional, one-dimensional would be faster as it's an array lookup and a primitive vs an array lookup, a dereference of a reference of an array object, and then a lookup in that array.
However, such microoptimization is not necessarily the best use of your time, as there are likely better places for improvements.

Categories