我有一个int类型的数组
矩阵[][]
它的值为0和1
000000000
101010111
000010100
110011001
Run Code Online (Sandbox Code Playgroud)
这些值与上述不同,但这是一个随机的例子.
我需要做的是,
当column = 0和row = 0时,循环遍历第一行,将row添加到第一行
如果找到一个,则将其添加到变量中
当我到达行的末尾时,我需要沿着colum 0 row = 0将1添加到列以获取循环
那么我需要检查我一直在添加的和变量是%2 = 0
然后我需要检查第1行第1列
并重复所有
我遇到的问题是确定我何时到达行的末尾,这是如何计算的?
for(int row = 0; row < matrix.length; row++){
if(matrix[columns][row] == 1){
sum ++;
if(i am at the end of the row){
//continue with other steps here
Run Code Online (Sandbox Code Playgroud)
for (int row = 0; row < matrix.length; row++) {
int sum = 0;
for (int col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] == 1){
sum ++;
}
}
// reached the end of the row
}
// reached the end of the array
Run Code Online (Sandbox Code Playgroud)
因此,对于每一行(第一行),迭代每行(第二行)中的列.这将涵盖2d阵列中的所有元素.你知道你已经到了行的末尾,因为你已经用完了列(并退出了内循环).
| 归档时间: |
|
| 查看次数: |
15034 次 |
| 最近记录: |