java - 翻转多维数组java

标签 java multidimensional-array

<分区>

我目前正在研究俄罗斯方 block AI,我正在寻找一种方法来翻转 4 x 4 多维数组。我到处都看了,我能找到的最多的是旋转,这对我来说是行不通的。 来自

o o o o 

o x x o

o x o o

o x o o 

o x o o

o x o o

o x x o

o o o o

最佳答案

我不知道你需要翻转哪个维度,但这是其中之一......请注意,此方法会破坏原始数组!你没有说清楚你的需求。

  • 你没有说需要翻转哪个维度
  • 你没有说它应该就地还是创建一个新数组

也就是说,这是一个解决方案

public static void main(String args[]) {
    Integer[][] myArray = {{1, 3, 5, 7},{2,4,6,8},{10,20,30,40},{50,60,70,80}};

    // Before flipping  
    printArray(myArray);
    System.out.println();

    // Flip
    flipInPlace(myArray);

    // After flipping
    printArray(myArray);
}

public static void printArray(Object[][] theArray) {
    for(int i = 0; i < theArray.length; i++) {
        for(int j = 0; j < theArray[i].length; j++) {
            System.out.print(theArray[i][j]);
            System.out.print(",");
        }
        System.out.println();
    }
}

// *** THIS IS THE METHOD YOU CARE ABOUT ***
public static void flipInPlace(Object[][] theArray) {
    for(int i = 0; i < (theArray.length / 2); i++) {
        Object[] temp = theArray[i];
        theArray[i] = theArray[theArray.length - i - 1];
        theArray[theArray.length - i - 1] = temp;
    }
}

产生:

1,3,5,7,
2,4,6,8,
10,20,30,40,
50,60,70,80,

50,60,70,80,
10,20,30,40,
2,4,6,8,
1,3,5,7,

关于java - 翻转多维数组java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13279740/

相关文章:

java - 将服务添加到具有困难包结构的 Android list (LibGDX)/在 Android list 中引用包

java - JAX-RS/Jersey : java. lang.ClassCastException - 无法转换为 javax.xml.bind.JAXBElement

java - Android Intent 和这个新 Intent 的对象(实例)

java - 为什么 JComboBox 上的 getSelectedItem() 不是通用的?

c++ - 在不使用任何循环的情况下动态分配二维数组?

C++ 多维字符串数组初始化 (std::map)

Java Set - 如何根据名称列表进行排序

javascript - 多维数组indexOf不工作js

java - 如何声明 imageIcon 数组?

php - 从php中的多维数组中删除具有空值的行