I am trying to use jagged array to determine if the matrix is upper or lower triangle.
here is what I have.
The problem is the method returns true for both upper and lower triangular.
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static boolean isUpper(int[][] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array.length == array[i].length){
return true;
}
}
}
System.out.println("false");
return false;
}
public static void main(String args[]) {
int myA[][] = {{3,4,5},{6,7},{8}};
int myB[][] = {{8},{6,7},{5,4,3}};
isUpper(myB);
}
}
答案 0 :(得分:1)
Checking the length with the first row should return true for myA and false for myB
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Jagged {
public static boolean isUpper(int[][] array){
for (int i = 0; i < array.length; i++) {
if(array.length == array[0].length){
System.out.println("yes");
return true;
}
}
System.out.println("no");
return false;
}
public static void main(String args[]) {
int myA[][] = {{3,4,5},{6,7},{8}};
int myB[][] = {{8},{6,7},{5,4,3}};
isUpper(myA);
}
}
答案 1 :(得分:0)
The code returns true when the `length` of any inner Array is the same as the `length` for the containing array. Both arrays (`myA` and `myB`) have a length of `3`, so that means that if any of the inner arrays have a length of `3` it will `return true`. `myA` and `myB` both have an inner array with a length of 3 (`{3,4,5}` for `myA` and `{5,4,3}` for `myB`)so both will return `true`.