比较2d数组java中的元素?

时间:2015-12-22 21:42:29

标签: java arrays

如果我在Java中有二维数组:

//METHOD 1
//CHANGING THE THEME DYNAMICALLY HIDES THE SIDEMENUBAR WHEN I'VE SIMPLY 
//ADDED COMMANDS LIKE THIS
current.addCommand(new Command("Home") {
    {
      putClientProperty("place", "side");
    }
});

//METHOD 2
//CHANGING THE THEME DYNAMICALLY DOES NOT HIDE THE SIDEMENUBAR WHEN I'VE
//USED toolbar.addComponentToSideMenu TO ADD BUTTONS WITH COMMANDS 
toolbar = new Toolbar();
current.setToolbar(toolbar);
Button home = new Button("Home");
toolbar.addComponentToSideMenu(home, new Command("Home"){

  @Override
  public void actionPerformed(ActionEvent evt) {
    wb.setURL(startURL);
  }
});

...

//I USED THE FOLLOWING CODE TO DYNAMICALLY SET THE THEME AFTER EVALUATING A 
//WebBrowser URI REGARDLESS OF WHICH METHOD WAS USED TO ADD COMMANDS
wb.setBrowserNavigationCallback(new BrowserNavigationCallback() {
  public boolean shouldNavigate(String url) {
    if ((url.indexOf("users/login") != -1)) {
        try {
            //theme_noside.res has hideLeftSideMenuBool set to true
            theme = Resources.openLayered("/theme_noside");
            UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
            UIManager.getInstance().getLookAndFeel().setMenuBarClass(SideMenuBar.class);
            Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
            current.refreshTheme();
        }catch(IOException e){
            Log.p(e.toString());
        }
    }
    else {
        try {
            //theme.res has hideLeftSideMenuBool set to false
            theme = Resources.openLayered("/theme");
            UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
            UIManager.getInstance().getLookAndFeel().setMenuBarClass(SideMenuBar.class);
            Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
            current.refreshTheme();
        }catch(IOException e){
            Log.p(e.toString());
        }
    }
    return true;
  }
});

我还有另一个较小或相当的尺寸:

1 0 0
0 1 0
1 0 1

我如何找到第一个数组中的值区域等于第二个数组的匹配项?

例如,如果我们将第一个数组拆分成多个不同的小数组,每个数组都与第二个数组具有相同的维度......

这将是左上角:

1 0
0 1

这是右上角:

1 0
0 1

等等......

如何检查第一个数组的一个分裂是否等于第二个数组

这是我用来定义数组的代码:

0 0
1 0

然后我尝试使用public static void main(String argv[]) { int a[][] = { {1,0,0}, {0,1,0}, {1,0,1} }; int element[][] = {{1,0}, {0,1}}; } 来比较它们。

1 个答案:

答案 0 :(得分:1)

您可以通过查看[x,y]到[x + 1,y + 1]中的值来在x和y处剪切数组。

public static int[][] cut(int[][] source, int x, int y)
{
    return new int[][]{
        new int[]{ source[x][y], source[x + 1][y] },
        new int[]{ source[x][y + 1], source[x + 1][y + 1] }
    };
}

注意此函数假设您的数组是方形的。然后迭代大数组,剪切和比较。由于我们的cut函数采用2x2数组,因此我们在x和y到达大数组的边缘之前停止(因此x< large.length - 1)。

public static boolean test(int[][] large, int[][] small)
{
    for (int x = 0; x < large.length - 1; x++)
        for (int y = 0; y < large[0].length - 1; y++)
        {
            int[][] part = cut(large, x, y);
            if (Arrays.deepEquals(part, small))
                return true;
        }
}