简体   繁体   English

遍历2D数组

[英]Iterating through 2D Arrays

I need to create a method that will take a specific index in a 2D array and print out the values of the neighboring cells in clockwise order. 我需要创建一个方法,该方法将在2D数组中使用特定的索引,并按顺时针顺序打印出相邻单元的值。 I am not sure if I have to hardcode this program with a bunch of "If" statements or is there a looping way to achieve it? 我不确定是否必须使用一堆“ If”语句对该程序进行硬编码,或者是否有循环方法来实现它?

The method takes in two parameters (rows and columns) and returns a String giving the values with a comma in between. 该方法接受两个参数(行和列),并返回一个字符串,给出两个字符串之间的逗号。

I'm guessing you want something like this (constructor etc. omitted): 我猜你想要这样的东西(构造函数等。省略):

public class weird_array_class_of_doom{

    public array[][] the_thing;

    public void print_the_cells(int row, int col){
        System.out.print(array[row][col+1];
        System.out.print(array[row+1][col];
        System.out.print(array[row][col-1];
        System.out.print(array[row-1][col];
    }
}

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

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