在二维数组中查找数字的位置

时间:2017-02-17 16:06:22

标签: java arrays multidimensional-array

我正在尝试创建一个程序,让您可以找到2d数组中窗帘编号出现的次数。我需要做的一件事就是创建一个名为countInstence的方法,它计算一个数字出现的次数,以及它们在数组中的位置。我遇到的问题是如何输出数字的位置

public int[][] createArray(int rSize, int cSize) {

    Random rnd = new Random();
    int[][] array = new int[rSize][cSize];

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            array[row][col] = rnd.nextInt(26);
        }
    }
    return array;
}

public void print2dArray(int[][] array) {

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            System.out.print(array[row][col] + "\t");
        }
        System.out.println("\n");
    }
}

public int countInstence(int[][] array, int search) {

    int count = 0;

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            if (array[row][col] == search)
                count++;

        }
    }
    return count;
}

public static void main(String[] args) {

    Journal5b call = new Journal5b();
    Scanner in = new Scanner(System.in);
    int[][] myArray;
    int value;
    myArray = call.createArray(10, 10);

    call.print2dArray(myArray);

    System.out.println("Enter a number to search for: ");
    value = in.nextInt();

    System.out.println("Your number occurred "
            + call.countInstence(myArray, value) + " Times");
}

4 个答案:

答案 0 :(得分:2)

只需在if-statement和格式中插入一行print语句即可打印当前索引的行和列,如下所示......

public int countInstance(int[][] array, int search) {

    int count = 0;

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            if (array[row][col] == search) {
                System.out.printf("Instance found at [%d, %d]\n", row, col);
                count++;
            }
        }
    }

    return count;
}

如果您不熟悉System.out.printf来电或%d符号,可以详细了解here

答案 1 :(得分:0)

只需将此行添加到for循环

即可
  for (int row = 0; row < array.length; row++) {
    for (int col = 0; col < array[0].length; col++) {
        if (array[row][col] == search) {
            System.out.printf("Instance found at [%d, %d]\n", row, col);// add this line 
            count++;
        }
    }
}

答案 2 :(得分:0)

如果您还希望在其他地方使用位置,则只需声明一个列表,并在搜索元素时向该列表添加位置。

  public static List<String> location=new ArrayList<String>();
  public int countInstence(int[][] array, int search) {

    int count = 0;

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            if (array[row][col] == search)
            {
                count++;
                String loc=row+","+col;
                location.add(loc);
            }
        }
    }
    return count;
}

public static void main(String[] args) {

    Test call = new Test();
    Scanner in = new Scanner(System.in);
    int[][] myArray;
    int value;
    myArray = call.createArray(10, 10);

    call.print2dArray(myArray);

    System.out.println("Enter a number to search for: ");
    value = in.nextInt();

    System.out.println("Your number occurred "
            + call.countInstence(myArray, value) + " Times");
    System.out.println("locations :");
    for(int i = 0; i < location.size(); i++) {
        System.out.println(location.get(i));
    }

}

答案 3 :(得分:0)

我建议使用积分:

 List<Point> locations=new ArrayList<Point>();
        ....
         if (array[row][col] == search)
         {
           count++;
           locations.add(new Point(row,col));
         }