【问题标题】:getting the edge value of a 2D array while preventing getting out of bounds获取二维数组的边缘值,同时防止越界
【发布时间】:2013-02-06 22:31:07
【问题描述】:

我在这里有 4x4 字符数组,我需要获取位于数组边缘的字符的共同值...我尝试了与我的问题类似的其他问题的解决方案,但我仍然得到相同的结果错误。,

这是我的代码..

//arr2[][]
//      arr2[3][0] = 'H';
//      arr2[3][1] = 'E';
//      arr2[3][2] = 'L';
//      arr2[3][3] = 'P';
//arr3[][]
//      arr3[1][3] = 'T';
//      arr3[2][3] = 'O';
//      arr3[3][3] = 'P';
//I specifically need the get the 'P' at [3][3]..
for(o = 0;o<count;o++){
        char letter = out.charAt(o);                        
        for(int m = 0; m < 4; m ++){    
            for(int n = 0; n < 4; n ++){
                if(Arrays.asList(arr3[m][n]).contains(letter)){ 
                    r = m;
                    c = n;
                }
            }
        }
        right  = arr2[r][c+1];
        left  = arr2[r][c-1];
        up  = arr2[r-1][c];
        down  = arr2[r+1][c];
        if(o==0){
                if(c==0){
                    if(r==0||r==3){
                        if(right!=null){
                            l = right;
                        }
                    }else{
                        if(right!=null){
                            l = right;
                        }else if(up!=null){
                            l = up;
                        }
                    }
                }else if(c==3){
                    if(r==0||r==3){
                        if(left!=null){
                            l = left;
                        }
                    }else{
                        if(left!=null){
                            l = left;
                        }else if(up!=null){
                            l = up;
                        }
                    }
                }else{
                    if(r==0||r==3){
                        if(left!=null){
                            l = left;
                        }else if(right!=null){
                            l = right;
                        }
                    }else{
                        if(left!=null){
                            l = left;
                        }else if(right!=null){
                            l = right;
                        }else if(up!=null){
                            l = up;
                        }
                    }
                }
            }
        }else if(o==(count-1)){
            if(vertical == 1){
                if(c==0){
                    if(r==0||r==3){
                        if(right!=null){
                            l = right;
                        }
                    }else{
                        if(right!=null){
                            l = right;
                        }else if(down!=null){
                            l = down;
                        }
                    }
                }else if(c==3){
                    if(r==0||r==3){
                        if(left!=null){
                            l = left;
                        }
                    }else{
                        if(left!=null){
                            l = left;
                        }else if(down!=null){
                            l = down;
                        }
                    }
                }else{
                    if(r==0||r==3){
                        if(left!=null){
                            l = left;
                        }else if(right!=null){
                            l = right;
                        }
                    }else{
                        if(left!=null){
                            l = left;
                        }else if(right!=null){
                            l = right;
                        }else if(down!=null){
                            l = down;
                        }
                    }
                }
            }
        }else{
            if(vertical == 1){
                if(c==0){
                    if(right!=null){
                        l = right;
                    }
                }else if(c==3){                                 
                    if(left!=null){
                        l = left;
                    }
                }else{
                    if(right!=null){
                        l = right;
                    }else if(left!=null){
                        l = left;
                    }
                }
            }
        }
        k = Character.toString(letter);
        letr = Character.toString(l);

【问题讨论】:

  • 你为什么写if(Arrays.asList(arr3[m][n]).contains(letter)){而不是if (arr3[m][n] == letter){??
  • @jlordo 我在这个程序之上还有其他程序,我从 arr3 的值创建了一个字符串,它是 TOP..我想知道每个字符的位置以及这样做的方法是检查它是否包含在 arr3[m][n]...
  • 我猜你没有得到任何答案,因为你的代码阅读起来过于复杂。许多if、else if 和else 案例,具有单字母变量名称。你说//I specifically need the get the 'P' at [3][3].. 这可以通过单行arr3[3][3] 来完成。我不懂你的代码,也不懂你的问题。至于我之前的评论,这两个条件是相同的,而我的很容易阅读和理解(而且效率更高),而你的则过于复杂。
  • 你需要数组四边的公共值吗?比如两个数组都有[0][3] == 'r',应该也能找到吧?
  • @jlordo 对不起,如果我的代码看起来很复杂,但如果你仔细看一下,你会发现它很简单......我正在尝试不同的方法,我只是这种语言的新手...你能教我一个更简单的方法吗...我的主要目标是在两个不同的数组中获得具有相同索引的共同字符...

标签: java multidimensional-array


【解决方案1】:

在您的代码中,

for(int m = 0; m < 4; m ++){    
    for(int n = 0; n < 4; n ++){
        if(Arrays.asList(arr3[m][n]).contains(letter)){ 
            r = m;
            c = n;
        }
    }
}

在这里,您正在分配 r 和 c,在某些情况下可能是 0 或 3,然后是下面的代码,

right  = arr2[r][c+1];
left  = arr2[r][c-1];
up  = arr2[r-1][c];
down  = arr2[r+1][c];

在上述分配中,您有c + 1、r + 1、c - 1、r - 1,如果前面的循环将 r 和 c 分配为 0 或 3,这肯定会超出范围。

【讨论】:

  • 女士,是的,这就是为什么我有很多 if else 语句来防止越界。,
  • 上述赋值甚至在那些 if 语句之前。
  • 所以我需要将这些语句放在 if 语句中?..我只是想把它放在那里,这样我就不需要重复声明它了..感谢您的帮助
  • 不,您需要确保 r 和 c 在执行这些任务之前不在其界限内。例如,right = c == 3 ? arr2[r][c] : arr2[r][c+1] 有效地表示如果 c 在边界上,不要递增而是按原样分配,否则 c + 1。同样,对于 r = 0 或 c = 0,您需要这样做。
  • 但是如果我需要查找特定元素周围的值...如果我不增加,我将无法获得该值
【解决方案2】:

您可以运行此程序并告诉我们是否有帮助吗?

char[][] arr2 = ... // your array
char[][] arr3 = ... // your other array
// inspect all common indeces:
for (int i = 0; i < arr2.length && i < arr3.length; i++) {
    for (int j = 0; j < arr2[i].length && j < arr3[i].length; j++) {
        // print value, if value is the same: 
        if (arr2[i][j] == arr3[i][j]) {
            // here you know that two characters on the same index are the same.
            // you have the information about the character and the indeces:
            System.out.println("Found common value at index (i,j)->"
                    + "(" + i + "," + j + "), char '" + arr2[i][j] + "'");
        }
    }
}

使用您评论示例中的数组,这将提供以下输出:

Found common value at index (i,j)-&gt;(3,3), char 'P'

这似乎是您正在寻找的。假设两个数组具有相同的维度,此解决方案会查看两个数组中的每个索引。如果一个数组大于另一个数组,则只查看公共索引。你提到了一些关于你的数组边缘的事情。我不清楚你的意思是只有边缘还是包括边缘。这个答案包括边缘。如果您只需要查看边缘,请随时在答案下方发表评论。

【讨论】:

  • 在我看来,OP 只想要数组 边缘 上的公共值......但它仍然需要她的确认。
  • @sp00m:如果 OP 确认我可以轻松调整我的答案;)
  • @kathy:很高兴为您提供帮助。请不要忘记接受最有帮助的答案。在这里查看它是如何工作的:How does accepting an answer work?。还请投票任何其他有用的答案,因为它可以帮助我们所有人赢得声誉。
【解决方案3】:

感谢那些帮助我解决这个问题的人。 我能够使用模数获得数组边缘的值..我能够防止超出范围...... https://stackoverflow.com/a/12743834/2078012 这个来自另一个问题的链接回答了我的问题.. 再次感谢您..:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2019-11-02
    • 2013-05-01
    • 1970-01-01
    • 2014-06-19
    • 2015-06-17
    • 2014-02-11
    相关资源
    最近更新 更多