需要帮助 Java 3D 数组替换特定坐标数据

huangapple go评论77阅读模式
英文:

Need help Java 3d Array replace Specific coordinate data

问题

public class practiceF {
    static int [][][]p = new int [10][9][ ];
    static int [][]PointDirection = {
        {3,5,0},
        {3,5,7,0},
        {5,7,0},
        {1,5,7,0},
        {1,7,0},
        {1,3,7,0},
        {1,3,0},
        {1,3,5,0},
        {3,4,5,7,0},
        {3,5,6,7,0},
        {1,2,3,5,7,0},
        {1,3,5,7,8,0},
        {1,3,4,5,7,0},
        {1,3,5,6,7,0},
        {1,2,3,7,0},
        {1,3,7,8,0},
        {1,2,3,4,5,6,7,8,0},
        {1,3,5,7,0}
    };

    public void textPrint() {
        for(int i=0;i<10;i++) {
            for(int j=0;j<9;j++) {
                System.out.print("|point"+i+"."+j+" : ");
                for(int k=0;k<p[i][j].length;k++) {
                    System.out.print(p[i][j][k]+" ");
                }
            }
            System.out.println("|");
        }
    }

    public static void main(String[] args) {

        practiceF pf = new practiceF();

        for(int i=0;i<10;i++) {
            for(int j=0;j<9;j++) {
                p[i][j] = Arrays.copyOf(PointDirection[17], PointDirection[17].length);
            }
        }

        pf.textPrint();
        System.out.println("-------------------");
        p[1][2][3] = 9;

        pf.textPrint();
    }
}

I hope this is the translation you were looking for. If you have any further questions or need more assistance, please feel free to ask.

英文:

I wanted to generate a board has some direction datas in each point.

    public class practiceF {
static int [][][]p = new int [10][9][ ];
static int [][]PointDirection = {
{3,5,0},
{3,5,7,0},
{5,7,0},
{1,5,7,0},
{1,7,0},
{1,3,7,0},
{1,3,0},
{1,3,5,0},
{3,4,5,7,0},
{3,5,6,7,0},
{1,2,3,5,7,0},
{1,3,5,7,8,0},
{1,3,4,5,7,0},
{1,3,5,6,7,0},
{1,2,3,7,0},
{1,3,7,8,0},
{1,2,3,4,5,6,7,8,0},
{1,3,5,7,0}
};
public void textPrint() {
for(int i=0;i&lt;10;i++) {
for(int j=0;j&lt;9;j++) {
System.out.print(&quot;|point&quot;+i+&quot;.&quot;+j+&quot; : &quot;);
for(int k=0;k&lt;p[i][j].length;k++) {
System.out.print(p[i][j][k]+&quot; &quot;);
}
}
System.out.println(&quot;|&quot;);
}
}
public static void main(String[] args) {
practiceF pf =new practiceF();
for(int i=0;i&lt;10;i++) {
for(int j=0;j&lt;9;j++) {
p[i][j]=PointDirection[17];//모든 칸 전후좌우 이동 지정
for(int k=0;k&lt;p[i][j].length;k++) {
}
}
}
pf.textPrint();
System.out.println(&quot;-------------------&quot;);
p[1][2][3]=9;
pf.textPrint();
}
}

I've only selected the problematic part of the entire code.
array p is actual database of board.
p[x coordinate][y coordinate][direction int] this is each int means.
PointDirection array is inserted in p array.

this process create board and insert some Direction int in each point.

in main, print board on console, and change Direction int of (1,2)point 0 to 9.
then, next print have to display change just Direction int of (1,2)point, but this code changes Direction int of all points.

this is result.

|point0.0 : 1 3 5 7 0 |point0.1 : 1 3 5 7 0 |point0.2 : 1 3 5 7 0 |point0.3 : 1 3 5 7 0 |point0.4 : 1 3 5 7 0 |point0.5 : 1 3 5 7 0 |point0.6 : 1 3 5 7 0 |point0.7 : 1 3 5 7 0 |point0.8 : 1 3 5 7 0 |
|point1.0 : 1 3 5 7 0 |point1.1 : 1 3 5 7 0 |point1.2 : 1 3 5 7 0 |point1.3 : 1 3 5 7 0 |point1.4 : 1 3 5 7 0 |point1.5 : 1 3 5 7 0 |point1.6 : 1 3 5 7 0 |point1.7 : 1 3 5 7 0 |point1.8 : 1 3 5 7 0 |
|point2.0 : 1 3 5 7 0 |point2.1 : 1 3 5 7 0 |point2.2 : 1 3 5 7 0 |point2.3 : 1 3 5 7 0 |point2.4 : 1 3 5 7 0 |point2.5 : 1 3 5 7 0 |point2.6 : 1 3 5 7 0 |point2.7 : 1 3 5 7 0 |point2.8 : 1 3 5 7 0 |
|point3.0 : 1 3 5 7 0 |point3.1 : 1 3 5 7 0 |point3.2 : 1 3 5 7 0 |point3.3 : 1 3 5 7 0 |point3.4 : 1 3 5 7 0 |point3.5 : 1 3 5 7 0 |point3.6 : 1 3 5 7 0 |point3.7 : 1 3 5 7 0 |point3.8 : 1 3 5 7 0 |
|point4.0 : 1 3 5 7 0 |point4.1 : 1 3 5 7 0 |point4.2 : 1 3 5 7 0 |point4.3 : 1 3 5 7 0 |point4.4 : 1 3 5 7 0 |point4.5 : 1 3 5 7 0 |point4.6 : 1 3 5 7 0 |point4.7 : 1 3 5 7 0 |point4.8 : 1 3 5 7 0 |
|point5.0 : 1 3 5 7 0 |point5.1 : 1 3 5 7 0 |point5.2 : 1 3 5 7 0 |point5.3 : 1 3 5 7 0 |point5.4 : 1 3 5 7 0 |point5.5 : 1 3 5 7 0 |point5.6 : 1 3 5 7 0 |point5.7 : 1 3 5 7 0 |point5.8 : 1 3 5 7 0 |
|point6.0 : 1 3 5 7 0 |point6.1 : 1 3 5 7 0 |point6.2 : 1 3 5 7 0 |point6.3 : 1 3 5 7 0 |point6.4 : 1 3 5 7 0 |point6.5 : 1 3 5 7 0 |point6.6 : 1 3 5 7 0 |point6.7 : 1 3 5 7 0 |point6.8 : 1 3 5 7 0 |
|point7.0 : 1 3 5 7 0 |point7.1 : 1 3 5 7 0 |point7.2 : 1 3 5 7 0 |point7.3 : 1 3 5 7 0 |point7.4 : 1 3 5 7 0 |point7.5 : 1 3 5 7 0 |point7.6 : 1 3 5 7 0 |point7.7 : 1 3 5 7 0 |point7.8 : 1 3 5 7 0 |
|point8.0 : 1 3 5 7 0 |point8.1 : 1 3 5 7 0 |point8.2 : 1 3 5 7 0 |point8.3 : 1 3 5 7 0 |point8.4 : 1 3 5 7 0 |point8.5 : 1 3 5 7 0 |point8.6 : 1 3 5 7 0 |point8.7 : 1 3 5 7 0 |point8.8 : 1 3 5 7 0 |
|point9.0 : 1 3 5 7 0 |point9.1 : 1 3 5 7 0 |point9.2 : 1 3 5 7 0 |point9.3 : 1 3 5 7 0 |point9.4 : 1 3 5 7 0 |point9.5 : 1 3 5 7 0 |point9.6 : 1 3 5 7 0 |point9.7 : 1 3 5 7 0 |point9.8 : 1 3 5 7 0 |
-------------------
|point0.0 : 1 3 5 7 9 |point0.1 : 1 3 5 7 9 |point0.2 : 1 3 5 7 9 |point0.3 : 1 3 5 7 9 |point0.4 : 1 3 5 7 9 |point0.5 : 1 3 5 7 9 |point0.6 : 1 3 5 7 9 |point0.7 : 1 3 5 7 9 |point0.8 : 1 3 5 7 9 |
|point1.0 : 1 3 5 7 9 |point1.1 : 1 3 5 7 9 |point1.2 : 1 3 5 7 9 |point1.3 : 1 3 5 7 9 |point1.4 : 1 3 5 7 9 |point1.5 : 1 3 5 7 9 |point1.6 : 1 3 5 7 9 |point1.7 : 1 3 5 7 9 |point1.8 : 1 3 5 7 9 |
|point2.0 : 1 3 5 7 9 |point2.1 : 1 3 5 7 9 |point2.2 : 1 3 5 7 9 |point2.3 : 1 3 5 7 9 |point2.4 : 1 3 5 7 9 |point2.5 : 1 3 5 7 9 |point2.6 : 1 3 5 7 9 |point2.7 : 1 3 5 7 9 |point2.8 : 1 3 5 7 9 |
|point3.0 : 1 3 5 7 9 |point3.1 : 1 3 5 7 9 |point3.2 : 1 3 5 7 9 |point3.3 : 1 3 5 7 9 |point3.4 : 1 3 5 7 9 |point3.5 : 1 3 5 7 9 |point3.6 : 1 3 5 7 9 |point3.7 : 1 3 5 7 9 |point3.8 : 1 3 5 7 9 |
|point4.0 : 1 3 5 7 9 |point4.1 : 1 3 5 7 9 |point4.2 : 1 3 5 7 9 |point4.3 : 1 3 5 7 9 |point4.4 : 1 3 5 7 9 |point4.5 : 1 3 5 7 9 |point4.6 : 1 3 5 7 9 |point4.7 : 1 3 5 7 9 |point4.8 : 1 3 5 7 9 |
|point5.0 : 1 3 5 7 9 |point5.1 : 1 3 5 7 9 |point5.2 : 1 3 5 7 9 |point5.3 : 1 3 5 7 9 |point5.4 : 1 3 5 7 9 |point5.5 : 1 3 5 7 9 |point5.6 : 1 3 5 7 9 |point5.7 : 1 3 5 7 9 |point5.8 : 1 3 5 7 9 |
|point6.0 : 1 3 5 7 9 |point6.1 : 1 3 5 7 9 |point6.2 : 1 3 5 7 9 |point6.3 : 1 3 5 7 9 |point6.4 : 1 3 5 7 9 |point6.5 : 1 3 5 7 9 |point6.6 : 1 3 5 7 9 |point6.7 : 1 3 5 7 9 |point6.8 : 1 3 5 7 9 |
|point7.0 : 1 3 5 7 9 |point7.1 : 1 3 5 7 9 |point7.2 : 1 3 5 7 9 |point7.3 : 1 3 5 7 9 |point7.4 : 1 3 5 7 9 |point7.5 : 1 3 5 7 9 |point7.6 : 1 3 5 7 9 |point7.7 : 1 3 5 7 9 |point7.8 : 1 3 5 7 9 |
|point8.0 : 1 3 5 7 9 |point8.1 : 1 3 5 7 9 |point8.2 : 1 3 5 7 9 |point8.3 : 1 3 5 7 9 |point8.4 : 1 3 5 7 9 |point8.5 : 1 3 5 7 9 |point8.6 : 1 3 5 7 9 |point8.7 : 1 3 5 7 9 |point8.8 : 1 3 5 7 9 |
|point9.0 : 1 3 5 7 9 |point9.1 : 1 3 5 7 9 |point9.2 : 1 3 5 7 9 |point9.3 : 1 3 5 7 9 |point9.4 : 1 3 5 7 9 |point9.5 : 1 3 5 7 9 |point9.6 : 1 3 5 7 9 |point9.7 : 1 3 5 7 9 |point9.8 : 1 3 5 7 9 |

i just want to change one point Direction int. not all point.
Idon't know what's wrong.

答案1

得分: 0

你将同一个实例 PointDirection[17] 赋值给了所有的点。因此对它所做的任何更改都会影响到所有的点。你应该将 PointDirection[17] 的内容复制到一个新的数组实例中,为每个点都创建一个新的数组实例。

可以尝试这样做:

p[i][j] = Arrays.copyOf(PointDirection[17], PointDirection[17].length);
英文:

You assigned the same instance PointDirection[17] to all the points. So any changes in it affect all the points. You should copy content of PointDirection[17] to a new array instance for each point.
Try this:

p[i][j] = Arrays.copyOf(PointDirection[17], PointDirection[17].length);

huangapple
  • 本文由 发表于 2020年9月11日 19:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63845917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定