How to remove the empty element in multi dimentional string array? - java

String[][] array = {{"abcd",""},{"asdf",""},{"",""},{"",""},{"",""},{"",""}};
I want to remove the {"",""} elements from the array.
How can I do it in Java?

Remove?
You cannot change the size of an existing array.
If you want to create a new array with only these elements, count the length of each array, create a new array based on those lengths, add elements to the new array.
String[][] array= {{"abcd",""},{"asdf",""},{"",""},{"",""},{"",""},{"",""}};
//Assuming you want a 1-D array
int valuesPresent = 0;
for (int i = 0; i < arrray.length; i++) {
for (int j = 0; i < arrray[i].length; i++) {
if (array[i][j] != "") {
valuesPresent++;
}
}
}
//Now you know how many values are there, so initialize a new array of that size
String[] answer = new String[valuesPresent];
//Now add all the values to it
int index = 0;
for (int i = 0; i < arrray.length; i++) {
for (int j = 0; i < arrray[i].length; i++) {
if (array[i][j] != "") {
answer[index] = array[i][j];
index++;
}
}
}
To get a 2-d array, easy to understand:
//Just reordered input so we can understand better
String[][] array= {{"abcd","zxcs"}, //Row 0, col 0 = abcd and col 1 = zxcs
{"asdf",""}, //Row 1, col 0 = asdf and col 1 = ""
{"",""}}; //Row 2, col 0 = "" and col 2 = ""
//Counts how many columns have values(are not equal to "") in each row
int rowsWithValues = 0; //Counts how many rows have at least 1 value. Here, 2
for (int row = 0; row < arrray.length; row++) {
for (int col = 0; col < arrray[row].length; col++) {
if (array[row][col] != "") {
rowsWithValues++; //Since there's a col with value for this row
break; //If any one value has been found, no need to check other cols
}
}
}
//Now we know how many rows we need in the result array: 2 (row 2 has no values)
String[][] result = new String[2][];
//We need to add the 2 rows with values now
int arrayIndex = 0; //Points to next empty index in result[][]
for (int row = 0; row < arrray.length; row++) {
int colsWithValues = 0; //Cols with values for each row
for (int col = 0; col < arrray[i].length; col++) {
if (array[row][col] != "") {
colsWithValues ++; //Col with value found for this row
}
}
//Eg. For row 0, colsWithValues will be 2, since 2 cols have values(abcd, zxcs)
//We need to add these 2 cols as a single array to result
String[] currentRow = new String[colsWithValues]; //Will be 2 here for row 0
int rowIndex = 0; //Points to next empty column in currentRow[]
for (int col = 0; col < array[row].length; col++) {
if (array[row][col] != "") {
currentRow[rowIndex] = array[row][col];
}
}
//After this, for row 0, currentRow will be {abcd, zxcs}
//Just add it to our result
result[arrayIndex] = currentRol;
//After 1st iteration, result will be {{"abcd", "zxcs"}, {}}
//During 2nd iteration, arrayIndex == 1, currentRow == {"asdf"}
//On adding it to result[1], result will be {{"abcd", "zxcs"}, {"asdf"}}

To start with, don't compare two Strings for equality using == or !=, even for String Arrays:
if (array[i][j] != "") {
In the case above, it should be:
if (!array[i][j].equals("")) {
If you're not quite up to Streams yet then this is one way that might interest you:
public static String[][] removeNullStringRows(String[][] array) {
if (array == null || array.length == 0) {
return null;
}
int validCount = 0; // Row Index Counter for the new 2D Array
/* Find out how may rows within the 2D array are valid
(where the do not consist of Null Strings {"", ""}).
This let's you know how many rows you need to initialize
your new 2D Array to. */
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (!array[i][j].equals("")) {
validCount++;
break;
}
}
}
/* Declare and initialize your new 2D Array. This is
assuming the column count is the same in all rows. */
String[][] array2 = new String[validCount][array[0].length];
validCount = 0; // Used as an index increment counter for the new 2D Array
// Iterate through the supplied 2D Array and weed out
// the bad (invalid) rows.
for (int i = 0; i < array.length; i++) { // Iterate Rows...
for (int j = 0; j < array[i].length; j++) { // Iterate Columns
/* Does this row contain anything other than a Null String ("")?
If it does then accept the entire Row into the new 2D Array. */
if (!array[i][j].equals("")) {
// Retrieve all the columns for this row
for (int k = 0; k < array[i].length; k++) {
array2[validCount][k] = array[i][k];
}
// The above small 'for' loop can be replaced with:
// System.arraycopy(array[i], 0, array2[validCount], 0, array[i].length);
validCount++; // Increment our Row Index Counter for the new 2D Array
break; // Get out of this column iterator. We already know it's good.
}
}
}
return array2; // Return the new 2D Array.
}
To use this method you might do it this way:
// Your current 2D Array
String[][] array = {
{"abcd",""}, {"asdf",""}, {"",""},
{"",""}, {"",""}, {"",""}
};
// If the supplied 2D Array is null contains no rows
// then get out of here.
if (array == null || array.length == 0) {
return;
}
// Display the original 2D Array (array) in the Console window
System.out.println("The original 2D Array:");
for (int i = 0; i < array.length;i++) {
System.out.println(Arrays.toString(array[i]));
}
// Remove all rows that contain all Null String Columns.
// Make your Array equal what is returned by our method.
array = removeNullStringRows(array);
// Display the new 2D Array (array) in the Console window.
System.out.println();
System.out.println("The New 2D Array:");
for (int i = 0; i < array.length;i++) {
System.out.println(Arrays.toString(array[i]));
}
And your Console Window output should look like:
The original 2D Array:
[abcd, ]
[asdf, ]
[, ]
[, ]
[, ]
[, ]
The New 2D Array:
[abcd, ]
[asdf, ]

I am assuming here that, the nested array could be of any size not just of 2 elements.
Create a predicate that takes Stream<String> and checks if any element is not null and non-empty.
String[][] array= {{"abcd",""},{"asdf",""},{"",""},{"",""},{"",""},{"",""}};
Predicate<Stream<String>> arrayPredicate = element ->
element.anyMatch(ele ->Objects.nonNull(ele) && !ele.isEmpty());
Now stream over the original array and filter the inner array based on a predicate and collect it in a new array.
String[][] copyArray = Arrays.stream(array)
.filter(arr -> arrayPredicate.test(Arrays.stream(arr)))
.toArray(String[][]::new);
array = copyArray; // reassign to array

You can filter out all null and empty elements from this 2d array as follows:
String[][] array = {{"abcd",""},{"asdf",""},{"",""},{"",""},{"",""},{"",""}};
String[][] nonEmptyArray = Arrays.stream(array)
.map(row -> Arrays.stream(row)
// filter out 'null' elements and empty strings
.filter(e -> e != null && e.length() > 0)
// an array of non-empty strings or an empty
// array if there are no such strings
.toArray(String[]::new))
// filter out empty arrays
.filter(row -> row.length > 0)
// an array of non-empty arrays
.toArray(String[][]::new);
// output
System.out.println(Arrays.deepToString(nonEmptyArray)); // [[abcd], [asdf]]

private String[][] removeFromArray(String[][] source, String[] objToRemove) {
return Arrays.stream(source)
.filter(element -> !Arrays.equals(element , objToRemove))
.toArray(String[][]::new);
}
void example() {
final String[] empty = new String[]{"", ""};
String[][] array = {{"abcd", ""}, {"asdf", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}};
array = removeFromArray(array, empty);
}
Something like that should work

Related

How to count the numbers of letters for each column in a String [][]?

I want to count the number of letters in a String [][] by column , so far my code is this :
for(int j = 0 ; j<matrix[0].length ;j++){
for(int i = 0 ; i< matrix.length ;i++ )
if (Character.isLetter(matrix[j][i].charAt(j)))
countChar++;
}
System.out.println(countChar + "letters");
return countChar;
but the output of the program counts how many elements the string has
for example if the String is :
String [][] C = {
{"abc", "abcd" , "abcd"},
{"oroeo", "kakakak" , "alsksjk"},
{"abcdef", "asdasdasdasd", "asdasdasdasd"},
};
the result is 9 , but should be 14 (number of letters by column )
Any help is greatly apreciated thank you !
You can define a 2D matrix as an array of rows or an array of columns. I'm assuming you have defined it as an array of rows and now want to get the values in a certain column.
So your data looks like this:
abc abcd abcd
oroeo kakakak alsksjk
abcdef asdasdasdasd asdasdasdasd
three rows and three columns.
to get for example the values in the middle column (with index 1) you would need to get the array elements:
matrix[0][1]
matrix[1][1]
matrix[2][1]
I think you are trying to count the total of the lengths of all values in each column. That would go like this:
// assume that the matrix has at least one row and thas the same number of columns in each row
// take the number of columns in the first row for reference
int numberOfColumns = matrix[0].length;
for(int col = 0; col < numberOfColumns; col++) {
int count = 0;
// iterate over all the rows
for(String[] row : matrix) {
// count the length of the element in position col of this row
count += row[col].length();
}
System.out.printf("%s characters in column %s", count, col);
}
int n = 0;
// iterate row by row
for (int i = 0; i < C.length; i++) {
n += C[i][0].length();
// get the string at index 0 (or 1 or 2.. whichever you want) of the array and append its length
// if you expect the string to contain numbers, then
// run a for-loop on the string and check if its a letter
}
System.out.println(n);
Try below the problem is with your for loop for which the no. of iterations are limited to the size of your matrix:
for(int i = 0 ; i<C[0].length ;i++) {
String matrixElement = C[i][0];
System.out.println(matrixElement);
for(int k =0 ;k < matrixElement.length();k++)
if (Character.isLetter(matrixElement.charAt(k)))
countChar++;
}
Please, format out your code and brush up the loop:
private static int countByColumn(String[][] matrix, int column) {
if (column < 0)
return 0; // Or throw exception
int countChar = 0;
for (String[] line : matrix) {
//DONE: jagged array: it may appear that the line is too short
if (line.length <= column)
continue;
String item = line[column];
for (int i = 0; i < item.length; ++i)
if (Character.isLetter(item.charAt(i)))
countChar += 1;
}
return countChar;
}
Test:
// 14
int test = countByColumn(C, 0);

Generating 2D double array from text file

I have a text file
0.4658 0 3
0.4095 0 3
0.4594 0 3
0.4297 0 3
0.3963 0 3
0.4232 0 3
0.4633 0 3
0.5384 0 3
0.5042 0 3
0.4328 0 3
that I want to read into a 2D double array that looks like this.
{{0.4658, 0, 3},
{0.4095, 0, 3},
... (and so on)
{0.4328, 0, 3}}
I have the following code:
public static void main(String[] args) throws Exception{
double[][] ref = null;
ref = matrix("data80.in",10,3);
}
public static double[][] matrix(String filename, int size1, int size2) throws Exception {
double[][] matrix = null;
BufferedReader buffer = new BufferedReader(new FileReader(filename));
String line;
int row = 0;
while ((line = buffer.readLine()) != null) {
String[] vals = line.trim().split("\\s+");
if (matrix == null) {
matrix = new double[size1][size2];
}
for (int col = 0; col < size1; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
row++;
}
buffer.close();
return matrix;
}
But it keeps giving me an outOfBounds exception, and I don't know where I am going wrong. Please help. If anyone has more efficient solutions as well to my problem it would be helpful
It's because of the following for loop:
for (int col = 0; col < size1; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
We are using size1 whereas we should be using size2, following would work:
for (int col = 0; col < size2; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
Also, there is no need for null check inside the for loop, you can remove it and initialise the array in the beginning, e.g.:
public static double[][] matrix(String filename, int size1, int size2) throws Exception {
double[][] matrix = new double[size1][size2];;
BufferedReader buffer = new BufferedReader(new FileReader(filename));
String line;
int row = 0;
while ((line = buffer.readLine()) != null) {
String[] vals = line.trim().split("\\s+");
for (int col = 0; col < size2; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
row++;
}
buffer.close();
return matrix;
}
You have defined you 2d matrix as
matrix = new double[size1][size2];
meaning there are size1 rows and size2 columns but in following line:
for (int col = 0; col < size1; col++) {
you have used size1. So correction is:
for (int col = 0; col < size2; col++) {
Here is yet another concept as to how you can place the delimited numerical data contained within a text file of any number of rows and any number of columns into a Double data type Two Dimensional Array. All you need to do is pass the path and file name to the method. You can also optionally supply the delimiter used within the file, the method default is a comma (,) delimiter since it is one of the most commonly used. Here is the method:
public static double[][] matrix(String filename, String... delimiterInFile) {
String delimiter = ","; // Default data delimiter in file.
// See if a optional data delimiter is supplied...
if (delimiterInFile.length > 0) { delimiter = delimiterInFile[0]; }
// Catch IO Exceptions
try {
//Place the contents of file into a ArrayList
List<String> list = Files.lines(Paths.get(filename)).collect(Collectors.toList());
// Get the greatest number of delimited columns contiained
// within the ArrayList the whole while ignoring blank lines
// (there could be blank lines in file). Our columns for our
// double 2D Array will be determined from this value. We also
// determine the true number of rows required (remember, no
// blank elements allowed so ArrayList.size() wont be good enough).
int r = 0, c = 0;
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals("")) {
int l = list.get(i).split(delimiter).length;
if (l > c) { c = l; }
r++;
}
}
// If we have nothing then the get outta here
// by returning null.
if (r == 0 || c == 0) { return null; }
// Declare and initialize a double data type 2D Array
double[][] array = new double[r][c];
// Fill the double type array...
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals("")) {
String[] data = list.get(i).split(delimiter);
for (int j = 0; j < data.length; j++) {
array[i][j] = Double.parseDouble(data[j]);
}
}
}
return array;
} catch (IOException ex) {
// Do what you want with the Exception...
ex.printStackTrace();
return null;
}
}
This method will automatically determine the required number of Rows and Columns for the returned Double data type 2D Array. The method ignores blank file lines so the required Rows needed for the returned Double 2D Array is determined with this in mind. The number of Columns value is determined by iterating through the data lines and detecting which data line contains the most delimited data. That column count is used for the entire 2D Array. This means then that file data lines which contain less columns will have their remaining Array Elements filled with a 0.0 double type value.
The optional delimiter that can be passed to this method can be any string or a RegEx (Regular Expression) string, for example: " " or "\\s+" or "," or ", " or "\t", etc.
This method will also throw an IO Exception should there be a problem accessing the supplied data file.
With the data file schema you provided:
0.4658 0 3
0.4095 0 3
0.4594 0 3
0.4297 0 3
0.3963 0 3
0.4232 0 3
0.4633 0 3
0.5384 0 3
0.5042 0 3
0.4328 0 3
and let's assume this file is named data.txt which is in your classpath, you might use this method like this:
double[][] myData = matrix("data.txt", "\\s+");
for (int i = 0; i < myData.length; i++) {
String strg = "";
for (int j = 0; j < myData[0].length; j++) {
strg+= myData[i][j] + " ";
}
System.out.println(strg);
}
Keep in mind, this is not a method I would recommend for really large data files (Hundreds of thousands of lines).

Displaying the string values of 2 dimensional Object array

I am trying to display the contents of an array after iterating through rows and columns of a JTable. I tried Arrays.toString(myTwoDimensionalArrayVariable) but it won't display the string values.
My goal is to check duplicates for every column per row of a destination JTable when user tries to add row values from a source JTable that's why I want to display the contents of the array.
The values on columns are combination of double, String, and int.
int myRowCount = aJTableParameter.getRowCount();
int myColumnCount = aJTableParameter.getColumnCount();
Object[][] myRowValues = new Object[myRowCount][myColumnCount];
for (int j = 0; j < myRowCount; j++) {
for(int i = 0; i< myColumnCount; i++){
myRowValues[j][i] = aDestinationTable.getValueAt(j, i);
}
}
System.out.println(Arrays.toString(myRowValues));
if (Arrays.asList(myRowValues).contains(column1Value)
&& Arrays.asList(myRowValues).contains(column2Value)
&& Arrays.asList(myRowValues).contains(column3Value)
&& Arrays.asList(myRowValues).contains(column4Value)) {
JOptionPane.showMessageDialog(null, "Duplicate, try again.");
}else{
//do something else
}
I only get this output:
run:
Successfully recorded login timestamp
[]
[[Ljava.lang.Object;#35fa3ff2]
[[Ljava.lang.Object;#407c448d, [Ljava.lang.Object;#1e78a60e]
Is there any other alternative than using 2 Dimensional Arrays?
I'd appreciate any help.
Thanks.
IFF your JTable cells contain only Strings, you can define your array as String[][] instead of Object[][] and fill it with your JTable contents using aDestinationTable.getValueAt(j, i).toString().
EDIT: since that's not the case (as per your comment), it's probably better to use a List, like this:
List<List<Object>> objectList = new ArrayList<>();
for (int j = 0; j < 2; j++) {
objectList.add(j, new ArrayList<>());
for (int i = 0; i < 2; i++) {
if (i==0) objectList.get(j).add("string" + j + i);
if (i==1) objectList.get(j).add((double) 37.8346 * j * i);
}
}
System.out.println("OBJECT LIST: "+objectList);
Output:
OBJECT LIST: [[string00, 0.0], [string10, 37.8346]]
Your code should look like this, then:
List<List<Object>> myRowValues = new ArrayList<>();
for (int j = 0; j < myRowCount; j++) {
myRowValues.add(j, new ArrayList<>());
for (int i = 0; i < myColumnCount; i++) {
myRowValues.get(j).add(aDestinationTable.getValueAt(j, i));
}
}
System.out.println(myRowValues);

Creating a multi-dimensional String array from a method array parameter

I am attempting to solve a semi-difficult problem in which I am attempting to create an array and return a 3 dimensional array based on the parameter which happens to be a 2 dimensional int array. The array I'm attempting to return is a String array of 3 dimensions. So here is the code:
public class Displaydata {
static String[][][] makeArray(int[][] dimensions) {
String myArray[][][];
for (int i = 0; i < dimensions.length; i++) {
for (int j = 0; j < dimensions[i].length; j++) {
myArray[][][] = new String[i][j][]; //getting error here.
}
}
return myArray;
}
static void printArray(String[][][] a) {
for (int i = 0; i < a.length; i++) {
System.out.println("\nrow_" + i);
for (int j = 0; j < a[i].length; j++) {
System.out.print( "\t");
for (int k = 0; k < a[i][j].length; k++)
System.out.print(a[i][j][k] + " ");
System.out.println();
}
}
}
public static void main(String[] args) {
int [][] dim = new int[5][];
dim[0] = new int[2];
dim[1] = new int[4];
dim[2] = new int[1];
dim[3] = new int[7];
dim[4] = new int[13];
dim[0][0] = 4;
dim[0][1] = 8;
dim[1][0] = 5;
dim[1][1] = 6;
dim[1][2] = 2;
dim[1][3] = 7;
dim[2][0] = 11;
for (int i = 0; i < dim[3].length;i++)
dim[3][i] = 2*i+1;
for (int i = 0; i < dim[4].length;i++)
dim[4][i] = 26- 2*i;
String[][][] threeDee = makeArray(dim);
printArray(threeDee);
}
}
As you can see from the source code, I'm getting an error when I try to create an instance of my 3-dimensional array which I'm attempting to return. I'm supposed to create a three dimensional array with the number of top-level rows determined by the length of dimensions and, for each top-level row i, the number of second-level rows is determined by the length of dimensions[i]. The number of columns in second-level row j of top-level row i is determined by the value of dimensions[i][j]. The value of each array element is the concatenation of its top-level row index with its second-level row index with its column index, where indices are represented by letters : ‘A’ for 0, ‘B’ for 1 etc. (Of course, this will only be true if the indices don’t exceed 25.) I don't necessarily know where I'm going wrong. Thanks!
You should not be initializing the array on every iteration of the loop. Initialize it once outside the loop and then populate it inside the loop.
static String[][][] makeArray(int[][] dimensions) {
String[][][] myArray = new String[25][25][1];
for (int i = 0; i < dimensions.length; i++) {
for (int j = 0; j < dimensions[i].length; j++) {
myArray[i][j][0] = i + "," + j;
}
}
return myArray;
}
I just plugged in values for the size of the first two dimensions, you will need to calculate them based on what you put in there. The 'i' value will always be dimensions.length but the 'j' value will be the largest value returned from dimensions[0].length -> dimensions[n-1].length where 'n' is the number of elements in the second dimension.
Also you will need to set up a way to convert the numbers in 'i' and 'j' to letters, maybe use a Map.
I guess you should initialize the array as
myArray = new String[i][j][]; //getting error here.
I think
myArray[][][] = new String[i][j][]; //getting error here.
should be:
myArray[i][j] = new String[5]; // I have no idea how big you want to go.
And then you can fill in each element of you inner-most array like such:
myArray[i][j][0] = "first item";
myArray[i][j][1] = "second string";
...
I think you should just change that line to:
myArray = new String[i][j][]; //look ma! no compiler error
Also, you would need to initialize myArray to something sensible (perhaps null?)

Copy 2D array and increase size to fill new values

I would like to first apologize if my question is worded badly. I have an exam tmmrw and the prof gave a sample final exam for us to practice with. He unfortunately isn't responding with the solutions on the forum so I am trying to provide solutions on there. I seem to be stuck on this question. I have to write a method that accepts and NxM array filled with integer values as a parameter. The method is to return an (N+1)x(M+1) array which contains the contents of the original array in the first N rows and M columns plus a count of items greater than or equal to zero in each row/column at the end of that row/column and put the value -1 in the bottom right corner. for example.
1 -2 0 returns 1 -2 0 2
3 -4 -5 3 -4 -5 1
2 0 1 -1
I seem to be able to copy the array yet I am puzzled as to how I can enter the values in the outer parts of the new array. Here is what I have so far.
public static void main(String[] args) {
int[][] arr = { { 1, -2, 0 }, { 3, -4, -5 } };
int[][] newMatrix = processing2D(arr);
printArray(newMatrix);
}
//Method where I am having problems
public static int[][] processing2D(int[][] arr) {
int[][] matrix = new int[arr.length][arr[0].length];
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
// once I reach the last pos I enter the count
// of numbers greater than or equal to zero in that row/col
matrix[row][col] = arr[row][col];
}
}
// assign the corner -1 here
return matrix;
}
public static void printArray(int[][] list) {
for (int row = 0; row < list.length; row++) {
for (int col = 0; col <= list.length; col++) {
System.out.print(list[row][col] + " ");
}
System.out.println();
}
}
First off you are initializing the new array wrong it should be
int[][] matrix = new int[arr.length+1][arr[0].length+1];
You don't want it to be the same length you want it to be the length +1. Also in your for loops you want to go by the length of arr not matrix since thats what you're taking from. While putting the values into the new N+1xM+1 array, increment the value of the corresponding last element in that row and column by 1 if it is >=0.
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
// once I reach the last pos I enter the count
// of numbers greater than or equal to zero in that row/col
if(arr[row][col]>=0){
matrix[row][matrix[row].length-1] = matrix[row][matrix[row].length-1] + 1;
matrix[matrix.length-1][col]= matrix[matrix.length-1][col] + 1;
}
matrix[row][col] = arr[row][col];
}
After putting all the values back into the new N+1xM+1 array you should now take the values in the n sized and m sized arrays and put them into the corresponding slot in the N+1xM+1 array. After that just put the -1 in the bottom right slow manually.
matrix[matrix.length-1][matrix[0].length-1]=-1;
In your process2D method start off by creating an array with the correct size which has 1 more row and 1 more column than the original:
int[][] matrix = new int[arr.length+1][arr[0].length+1];
Then to populate the matrix array you do like you were doing before, but you need to take care not to reference an index of the arr array that is out of bounds. Because your matrix index is bigger than arr. If you are populating the new indexes then you can just use random numbers.
if(row < arr.length && col < arr[0].length)
{
matrix[row][col] = arr[row][col];
}
else
{
matrix[row][col] = new Random().nextInt(10);
}
So here is the full method process2D:
public static int[][] processing2D(int[][] arr) {
int[][] matrix = new int[arr.length+1][arr[0].length+1];
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
// once I reach the last pos I enter the count
// of numbers greater than or equal to zero in that row/col
if(row < arr.length && col < arr[0].length)
{
matrix[row][col] = arr[row][col];
}
else
{
matrix[row][col] = new Random().nextInt(10);
}
}
}
// assign the corner -1 here
return matrix;
}

Categories