declaring a column length of 0 in a 2d array - java

I was just wondering if it was legal to declare a 2d ragged array with one or more of the columns having a length of 0. If it was legal, what will it actually do?

The answer is yes, it is possible. But then there's another question, why would you consider doing this? The reason I ask this is think about what you're trying to do here. An array in which the columns are of length 0 is technically an empty 2D array. I will, however, show how to do what you're asking and what will happen.
int[][] arr = new int[2][0]; //initializing a 2x0 array, which is 2 rows of size 0
or if you're trying to make a jagged array
int[][] arr = { {},{2} }; //this is a 2d array in which the first row has zero columns and the second row has one column
But what happens when you try operating on this array? You surprisingly won't get a compiler error, but you will get a runtime error. Suppose I added this line of code and tried running the program,
arr[0][0] = 1;
The error I would get is this,
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
because I can't access any elements in a 2d array with columns of size 0.

Related

How to Initialize 2d ArrayList with 0 in one line

How to Initialize all the element of 2d Array List in Java with 0 in single line of code without using for lop.
ArrayList of m rows and n columns.
How to initialise value of all the elements to 0 at the time of defining it.
Not sure what you meant by 2D ArrayList, but you returned type of List instead of ArrayList with help of below:
List<List<Integer>> a=Arrays.asList(Arrays.asList(0));
For normal arrays you can do something :
int[][] b=new int[1][2];

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.

How to Correctly Create a Set of a Multi-Dimensional Array

I have been looking through the Java Tutorials at the Interfaces tutorial, specifically on Collections (Set, List, Queue, etc.) and I came across the fact that a Set cannot contain duplicates in its elements.
My problem is the fact that I do not fully understand how to create a set of a multi-dimensional array of an unknown size.
In order to fill the multi-dimensional array, I will be placing 1's and 0's inside of an array so that each one will look like the following: (if it fits the criteria I am looking for)
[ 0 1 1 0
0 1 1 0
0 1 0 0
0 1 0 0
0 0 0 0 ]
Or something of that nature. I would like to think this can be accomplished through declaring an multi-dimensional array like:
int[][] array = new int[5][];
Yet I cannot understand how that would work with filling multiple array elements or how to accomplish this with a set.
Please let me know if this is not clear enough.
List's can contain duplicates, sets cannot. You can declare a (dynamic) multidimensional structure in several ways, heres one:
List<List<Integer>> multiDimensional = new ArrayList<List<Integer>>();
List<Integer> row = Arrays.asList({0, 1, 1, 0});
multiDimensional.add(row);
And so on and so forth. To access the elements of the list utilize the get method in a way similar to what you would do with arrays:
Integer someVal = multiDimensional.get(0).get(3);
Having said that, you only need to use this nested List setup if your multidimensional structure needs to be 100% dynamic, aka you need the ability to grow the rows and columns constantly throughout the execution of your logic. You can actually use an ordinary array for your multidimensional structure, assuming that the number of rows can be determined ahead of time, and that each row's length will not change after that row has been initialized. Case in point:
int[][] multiDimensional = null;
int rows = ... ;// Determine number of rows
multiDimensional = new int[rows][];
for(final int[] row: multiDimensional) {
final int cols = ...; // Determine number of cols for this row
row = new int[cols];
}
And you access the elements with your usual array semantics (multiDimensional[0][3]).

What is the reason for ArrayIndexOutOfBoundsException in my code?

I am implementing Graham Scan Algorithm for convex hull in Java.
I am getting this error while running the code. For input string: "10 18"
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Graham.SelectMin(Graham.java:110)
at Graham.GrahamScan(Graham.java:78)
at Graham.main(Graham.java:41)
Can anyone help me out to solve this error?
Thanks
java.lang.ArrayIndexOutOfBoundsException: 0
This means that you're trying to access an element of an empty array. (An array of size 0.)
You need to have a non-negative size of the array to be able to access element at index 0.
For reference, this code for instance, produces the same error:
int initialSize = 0;
int[] arr = new int[initialSize];
System.out.println(arr[0]);
it means that on line 110 of Graham.java you are trying to access an array index that is either < 0 or that is > the length of your array, can't tell exactly which array without code.
Also Graham.SelectMin(Graham.java:110) shows that you are naming method calls in a non-idiomatic Java way. In Java method calls should be in lowerCamelCase, such as Graham.selectMin();
In Java UpperCamelCase is reserved for ClassNames.

How to ask 2-dimensional Java array for its number of rows?

How should I go about asking a 2-dimensional array how many rows it has?
Firstly, Java technically doesn't have 2-dimensional arrays: it has arrays of arrays. So in Java you can do this:
String arr[][] = new String[] {
new String[3],
new String[4],
new String[5]
};
The point I want to get across is the above is not rectangular (as a true 2D array would be).
So, your array of arrays, is it by columns then rows or rows then columns? If it is rows then columns then it's easy:
int rows = arr.length;
(from the above example).
If your array is columns then rows then you've got a problem. You can do this:
int rows = arr[0].length;
but this could fail for a number of reasons:
The array must be size 0 in which case you will get an exception; and
You are assuming the length of the first array element is the number of rows. This is not necessarily correct as the example above shows.
Arrays are a crude tool. If you want a true 2D object I strongly suggest you find or write a class that behaves in the correct way.
Object[][] data = ...
System.out.println(data.length); // number of rows
System.out.println(data[0].length); // number of columns in first row
int[][] ia = new int[5][6];
System.out.println(ia.length);
System.out.println(ia[0].length);
It depends what you mean by "how many rows".
For a start, a 2-dimensional array is actually a 1-D array of 1-D arrays in Java. And there is no requirement that a 2-D array is actually rectangular, or even that all elements in the first dimension are populated.
If you want to find the number of elements in the first dimension, the answer is simply array.length.
If you want to find the number of elements in the second dimension of a rectangular 2-D array, the answer is `array[0].length.
If you want to find the number of elements in the second dimension of a non-rectangular or sparse 2-D array, the answer is undefined.

Categories