Cam*_*ron 5 java arrays nested-loops multidimensional-array
我是 Java 编程新手,我无法集中注意力解决我的一项作业中的最后一个问题。
我们被告知创建一个静态方法来搜索二维数组并将二维数组的数字与输入数字进行比较......就像这样:
私有静态 int[] searchArray(int[][] num, int N){
现在,我们返回的部分是一个新的一维数组,它告诉每行中第一个大于参数变量 N 的数字的索引。如果没有数字大于 N,则返回 -1数组的那个位置。
例如,名为“A”的多维数组:
4 5 6
8 3 1
7 8 9
2 0 4
如果我们使用此方法并执行 searchArray(A, 5),答案将是“{2,0,0,-1)”
通常,在使用多维数组时,您将使用嵌套的 for 循环:
for(int i = 0; i < outerArray.length; i++){
//this loop searches through each row
for(int j = 0; j < innerArrays.length; j++) {
//this loop searches through each column in a given row
//do your logic code here
}
}
Run Code Online (Sandbox Code Playgroud)
我不会给你更多的基本结构,因为你需要理解这个问题;将来您会经常遇到此类结构,但这应该可以帮助您入门。
| 归档时间: |
|
| 查看次数: |
2866 次 |
| 最近记录: |