【问题标题】:Return position of value in a 2D array, Java [closed]返回二维数组中值的位置,Java [关闭]
【发布时间】:2011-12-06 23:19:47
【问题描述】:

我累了。这一定很简单。木材……为……树木……

我正在尝试返回二维数组中特定值的位置。

我有一个双数组 [300][300]。

其中包含的所有值都是 0,除了一个是 255。

如何编写返回255的[i][j]位置的方法?

提前致谢。

【问题讨论】:

  • 这些值是否已排序?如果是这样,则有O(log n)(完全排序)或O(n log n)(仅行或列排序)版本。否则,您将被困在O(n^2) 上,正如所提供的那样。
  • @Clockwork-Muse 这些东西是什么意思。它们对我来说毫无意义,因此我不了解它们的价值
  • @JohnnyCoder - 它被称为Big O notation。它是对做某事所需的预期步骤数的简单度量。

标签: java arrays indexing 2d return


【解决方案1】:

简单地遍历所有元素,直到找到255

for ( int i = 0; i < 300; ++i ) {
    for ( int j = 0; j < 300; ++j ) {
        if ( array[i][j] == 255 ) {
            // Found the correct i,j - print them or return them or whatever
        } 
    }
}

【讨论】:

  • 您好,Shakedown 非常感谢您的帮助。我已经达到了这一点,但是在返回两个整数时遇到了麻烦。即如果 [150][200] 持有 255 我将如何返回这两个整数然后在其他地方使用?获得位置后,我需要将这两个用作 3D 数组 ([150][200][75]) 中的前两个位置。 /**你能说我是新手吗?**/
  • @OliverBurdekin:如果你想返回它们,你必须将它们放入一个数组或创建一个包含它们的对象。或者,您可以在该方法内部设置一些类变量(而不返回任何内容),尽管我不喜欢这样。另一种选择是将这些 i,j 变量留在该方法中,然后将它们传递给另一个方法,然后将它们用于进一步的索引。
  • @OliverBurdekin 关于如何返回找到的单元格的这对数字、列号和行号,在Java 16中定义自己的类变得更加容易,其中类的主要目的是进行通信数据透明且不可变:records:public record Cell ( int column , int row ) {}。编译器隐式创建构造函数、getter、equals & hashCodetoString。所以你的内部for 循环会说:return new Cell( j , i ) ;
【解决方案2】:

这行得通:

public int[] get255() {
  for(int i = 0; i < array.length; i++)
    for(int j = 0; j < (array[i].length/2)+1; j++)
      if(array[i][j] == 255)
        return new int[] {i,j};
      else if(array[j][i] == 255) //This just slightly increases efficiency
        return new int[] {j,i};
  return null; //If not found, return null
}

不过,这可能是最快的。它检查从每个角落开始,逐渐向内到中心,先水平,然后垂直:

public int[] get255() {
  for(int i = 0; i < (array.length/2)+1; i++)
    for(int j = 0; j < (array[i].length/2)+1; j++)
      // Check the top-left
      if(array[i][j] == 255)
        return new int[] {i,j};

      // Check the bottom-left
      else if(array[array.length-i][j] == 255)
        return new int[] {array.length-i,j};

      // Check the top-right
      else if(array[i][array[i].length-j] == 255)
        return new int[] {i,array[i].length-j};

      // Check the bottom-right
      else if(array[array.length-i][array[i].length-j])
        return new int[] {array.length-i, array[i].length-j};

  return null; //If not found, return null
}

【讨论】:

  • 如果数组中没有 255,这不会检查每个元素两次吗?
  • 是的,但如果您可以假设 255 将在数组中,那么它最多可以减少大约 25% 的时间。
  • 好吧,你可以只迭代 i=0..length/2j=0..length/2 来避免重复检查。 (可能会一个接一个)
  • @skyuzo 不知道为什么我忘记了,现在添加。还添加了.length/2+1 的安全性。不过,i 必须贯穿整个长度。
  • 这实际上可能并没有更有效。您仍在进行与 Shakedown 的答案 (n^2) 相同数量的比较。
【解决方案3】:

您可以对每一列使用二进制搜索。

使用 for 循环遍历每一列,因为它是一个二维数组。

从正在迭代的特定列(将是另一个数组)中获取所有行后,对它们进行二分搜索。

适用条件: 1.如果数组是排序的。 2. 如果您确定每列中只有一个元素。 (没有重复)。 3.如果行中有重复(做线性搜索)。

我希望它有所帮助。如果仍有疑问,请随时询问:)

【讨论】:

    【解决方案4】:

    对于任意大小的Array,你先把大小放好,虽然我是用扫描仪直接输入值的,你也可以从其他方法拿来,我正在学习,如有错误请指正。在您的情况下,值将是 255 而不是 1。

    public class MagicSquareOwn {
    
    
    
        public static int[] findValueByElement(int n) {
    
            Scanner input = new Scanner(System.in);
    
            int[][] squareMatrix = new int[n][n];
    
    
        int[] position = null;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    squareMatrix[i][j] = input.nextInt();
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
    
                    if (squareMatrix[i][j] == 1) {
                         position= new int[] { i, j };
                         System.out.println("position is:" + Arrays.toString(position));
                    }
                }
            }
    
            return position;
    
        }
    
        public static void main(String[] args) {
    
            Scanner inputOfMatrixNo = new Scanner(System.in);
            int n = inputOfMatrixNo.nextInt();
            MagicSquareOwn.findValueByElement(n);
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2013-02-25
      相关资源
      最近更新 更多