-3

说,如果我有一个二维数组:

int[][] s = {{1, 4, 5, 10, 11}, {2, 6, 8}, {3, 9, 12}, {7}}; 

我想编写一个方法来确保下一行的长度比数组的其余部分短我该怎么做?

基本上,如果没有行长于前一行,我将如何编写一个返回 true 的方法,否则返回 false ?

这是我的逻辑,即使我很遥远,只是不明白逻辑上如何去做。

 public static boolean rowDecrease(int[][] t){
     //result empty array here
     for (int j = 0; j < t.length; j++) {
      for (int i = 0; i< t[i].length; i++) { // Sum of Columns
        if (t[i].length>result){
          result = t[i].length;
          return false;
        }
        if (t[i].length<result){
          result = t[i].length;
          return true;
        }


        }

      }
4

3 回答 3

0

我认为与此类似的代码可以解决您的问题:

boolean resultado = true;

for(int i = 1; i < s.length; i++){
    if(s[i].length >= s[i-1].length) return false;
}

return true;
于 2016-04-07T06:49:42.053 回答
0

请在将来发布之前尝试或提出现有代码的问题。否则,我们将不得不开始向您发送发票。

public boolean shorterThanTheRest(int[][] s){
    int shortestLength = s[0].length + 1;
    for(int i = 0; i < s.length; i++)
        if(s[i].length < shortestLength)
            shortestLength = s[i].length;
        else
            return false;
     return true;
}
于 2016-04-07T06:49:53.090 回答
0

您可以使用Java Stream做到这一点:

int[][] s = {{1, 4, 5, 10, 11}, {2, 6, 8}, {3, 9, 12}, {7}};
List<Integer> S = Arrays.stream(s).map(i->i.length).collect(Collectors.toList());
boolean is = IntStream.range(0, S.size()).skip(1).allMatch(i->S.get(i)<=S.get(i-1));

我希望它有所帮助。

于 2016-04-07T06:51:54.320 回答