简体   繁体   English

检查任意大小的 Java 中二维数组的行和列中的元素

[英]Checking elements in rows and cols of 2D array in Java of arbitrary size

I am looking for a way to check the rows and the columns of 2D array of arbitrary size.我正在寻找一种方法来检查任意大小的二维数组的行和列。 For example, user wants to print NxN size board.例如,用户想要打印 NxN 尺寸的板。 In this case, we can't just check for the positions(rows and cols) as we do in fixed length of 2D array.在这种情况下,我们不能像在固定长度的二维数组中那样只检查位置(行和列)。 My first idea was to check each if array[i][j]==array[i][j+1], but this obviously won't work and will crash due to the ArrayOutOfBounds exception, as well as wont check the whole length of rows/cols.我的第一个想法是检查每个 if array[i][j]==array[i][j+1],但这显然行不通并且会由于ArrayOutOfBounds异常而崩溃,并且不会检查整个行/列的长度。

Any ideas/suggestions?有什么想法/建议吗?

My current flawed idea:我目前的错误想法:

for (int i = 0; i<arr.length-1; i++){
           for (int j = 0; j<arr[i].length-1; j++){
               if (arr[i][j]!=arr[i][j+1]){
                   //do something..
           }
       }

You need to make use of the Array.length property (it's a property, not a method, note,).您需要使用Array.length属性(注意,这是一个属性,而不是方法)。 to determine how wide your array is.来确定你的数组有多宽。 More examples etc. here更多示例等在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM