简体   繁体   English

如何在Java中使用fill(int [] a,int from_Index,int to_Index,int val)用于矩阵?

[英]how can I use fill(int[] a, int from_Index, int to_Index, int val) for matrix in Java?

I want to write a program for calculating the area covered on the screen by different windows. 我想编写一个程序来计算不同窗口在屏幕上覆盖的面积。 I have the number of windows and x1,y1,x2, y2 of each windows as input. 我有窗口的数量,每个窗口的x1,y1,x2,y2作为输入。 I want to use fill function in java. 我想在Java中使用填充功能。 I want to created a matrix the size of the desktop, full of zeroes. 我想创建一个桌面大小的矩阵,全为零。 For every window, I want to take the width of the window and input that into the matrix, for as many rows as the window height and do this for all windows, and then summarised the resulting matrix by counting the ones, giving me the area covered on the screen, without having to deal with overlapping windows. 对于每个窗口,我想获取窗口的宽度并将其输入到矩阵中,获取与窗口高度一样多的行,并针对所有窗口执行此操作,然后通过对所得的矩阵进行计数来汇总得出的矩阵,并为我提供面积覆盖在屏幕上,而不必处理重叠的窗口。 but I don't know how could I use fill for matrix. 但我不知道如何使用填充矩阵。

Suppose you have a 2-dim array like matrix[WIDTH][HEIGHT] . 假设您有一个2维数组,例如matrix[WIDTH][HEIGHT]
You can use the Arrays.fill() method to fill a whole column with a single call. 您可以使用Arrays.fill()方法通过一次调用来填充整列。 However, you still need to iterate over the columns. 但是,您仍然需要遍历各列。

I created a small program to illustrate. 我创建了一个小程序来说明。 Hope it helps: 希望能帮助到你:

import java.util.*;

public class TestArray
{
  public static final int SCREEN_WIDTH = 100;
  public static final int SCREEN_HEIGHT = 100;

  class WindowCoords
  {
    public int top;
    public int left;
    public int bottom;
    public int right;

    public WindowCoords(int top,
                        int left,
                        int bottom,
                        int right)
    {
      this.top = top;
      this.left = left;
      this.bottom = bottom;
      this.right = right;
    }
  } // class WindowCoords

  public List<WindowCoords> getWindows()
  {
    List<WindowCoords> result;
    result = new ArrayList<WindowCoords>();
    result.add(new WindowCoords(4, 67, 23, 89));
    result.add(new WindowCoords(18, 12, 65, 30));
    result.add(new WindowCoords(45, 3, 95, 15));
    result.add(new WindowCoords(67, 40, 93, 59));
    return (result);
  }

  public void run()
  {
    // Initialize matrix
    // Setting its contents to 0 not strictly necessary though I prefer to do so
    int[][] matrix;
    int     column;
    matrix = new int[SCREEN_WIDTH][SCREEN_HEIGHT];
    //for (column = 0; column < SCREEN_WIDTH; column++)
    //  Arrays.fill(matrix[column], 0);

    // Get windows
    List<WindowCoords> windows;
    windows = getWindows();

    // Fill covered screen
    Iterator<WindowCoords> it;
    WindowCoords           window;
    it = windows.iterator();
    while (it.hasNext())
    {
      window = it.next();
      for (column = window.left; column <= window.right; column++)
        Arrays.fill(matrix[column], window.top, window.bottom, 1);
    }

    // Show result
    int row;
    for (row = 0; row < SCREEN_HEIGHT; row++)
    {
      for (column = 0; column < SCREEN_WIDTH; column++)
        System.out.print(matrix[column][row]);
      System.out.println();
    }

  } // run

  public static void main(String[] args)
  {
    TestArray test;
    test = new TestArray();
    test.run();
  }

} // class TestArray

Just a simple nested loop will do: 只需一个简单的嵌套循环即可:

public static void main(String[] args) {
    int[][] matrix = new int[10][10];
    fill(matrix, 2, 2, 4, 4);

    System.out.println(Arrays.deepToString(matrix));
}

private static void fill(int[][] matrix, int x1, int y1, int x2, int y2) {
    for (int y = y1; y <= y2; y++) {
        for (int x = x1; x <= x2; x++) {
            matrix[y][x] = 1;
        }
    }
}

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

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