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

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

Need help Java 3d Array replace Specific coordinate data

问题

  1. public class practiceF {
  2. static int [][][]p = new int [10][9][ ];
  3. static int [][]PointDirection = {
  4. {3,5,0},
  5. {3,5,7,0},
  6. {5,7,0},
  7. {1,5,7,0},
  8. {1,7,0},
  9. {1,3,7,0},
  10. {1,3,0},
  11. {1,3,5,0},
  12. {3,4,5,7,0},
  13. {3,5,6,7,0},
  14. {1,2,3,5,7,0},
  15. {1,3,5,7,8,0},
  16. {1,3,4,5,7,0},
  17. {1,3,5,6,7,0},
  18. {1,2,3,7,0},
  19. {1,3,7,8,0},
  20. {1,2,3,4,5,6,7,8,0},
  21. {1,3,5,7,0}
  22. };
  23. public void textPrint() {
  24. for(int i=0;i<10;i++) {
  25. for(int j=0;j<9;j++) {
  26. System.out.print("|point"+i+"."+j+" : ");
  27. for(int k=0;k<p[i][j].length;k++) {
  28. System.out.print(p[i][j][k]+" ");
  29. }
  30. }
  31. System.out.println("|");
  32. }
  33. }
  34. public static void main(String[] args) {
  35. practiceF pf = new practiceF();
  36. for(int i=0;i<10;i++) {
  37. for(int j=0;j<9;j++) {
  38. p[i][j] = Arrays.copyOf(PointDirection[17], PointDirection[17].length);
  39. }
  40. }
  41. pf.textPrint();
  42. System.out.println("-------------------");
  43. p[1][2][3] = 9;
  44. pf.textPrint();
  45. }
  46. }

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.

  1. public class practiceF {
  2. static int [][][]p = new int [10][9][ ];
  3. static int [][]PointDirection = {
  4. {3,5,0},
  5. {3,5,7,0},
  6. {5,7,0},
  7. {1,5,7,0},
  8. {1,7,0},
  9. {1,3,7,0},
  10. {1,3,0},
  11. {1,3,5,0},
  12. {3,4,5,7,0},
  13. {3,5,6,7,0},
  14. {1,2,3,5,7,0},
  15. {1,3,5,7,8,0},
  16. {1,3,4,5,7,0},
  17. {1,3,5,6,7,0},
  18. {1,2,3,7,0},
  19. {1,3,7,8,0},
  20. {1,2,3,4,5,6,7,8,0},
  21. {1,3,5,7,0}
  22. };
  23. public void textPrint() {
  24. for(int i=0;i&lt;10;i++) {
  25. for(int j=0;j&lt;9;j++) {
  26. System.out.print(&quot;|point&quot;+i+&quot;.&quot;+j+&quot; : &quot;);
  27. for(int k=0;k&lt;p[i][j].length;k++) {
  28. System.out.print(p[i][j][k]+&quot; &quot;);
  29. }
  30. }
  31. System.out.println(&quot;|&quot;);
  32. }
  33. }
  34. public static void main(String[] args) {
  35. practiceF pf =new practiceF();
  36. for(int i=0;i&lt;10;i++) {
  37. for(int j=0;j&lt;9;j++) {
  38. p[i][j]=PointDirection[17];//모든 칸 전후좌우 이동 지정
  39. for(int k=0;k&lt;p[i][j].length;k++) {
  40. }
  41. }
  42. }
  43. pf.textPrint();
  44. System.out.println(&quot;-------------------&quot;);
  45. p[1][2][3]=9;
  46. pf.textPrint();
  47. }
  48. }

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.

  1. |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 |
  2. |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 |
  3. |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 |
  4. |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 |
  5. |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 |
  6. |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 |
  7. |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 |
  8. |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 |
  9. |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 |
  10. |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 |
  11. -------------------
  12. |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 |
  13. |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 |
  14. |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 |
  15. |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 |
  16. |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 |
  17. |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 |
  18. |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 |
  19. |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 |
  20. |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 |
  21. |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] 的内容复制到一个新的数组实例中,为每个点都创建一个新的数组实例。

可以尝试这样做:

  1. 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:

  1. 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:

确定