![](/img/trans.png)
[英]Java How to split array into two different arrays, and then add a number to the arrays
[英]Java - Split an array of arrays into multiple arrays
各位程序員,大家好,我在使用此邏輯時遇到麻煩,我想做的是在Android Studio應用程序(java)中將數組拆分成多個數組。
我有一個像這樣的數組:
int[][] goal = { {1, 2, 3}, {4, 5, 6}, {0, 7, 8} };
我希望它看起來像這樣:
int[] zero = {1,2,3};
int[] one = {4,5,6};
int[] two = {0,7,8};
但是我有麻煩,因為盡管我可以像這樣設置數組
int[] zero = goal [1][];
int[] one = goal [2][];
int[] two = goal [3][];
而且我不能,所以我要來這里了解你們中是否有人可以這樣做。
謝謝。
遵循你的邏輯...
更改此:
int[] zero = goal [1][];
int[] one = goal [2][];
int[] two = goal [3][];
為此:
int[] zero = goal [0];
int[] one = goal [1];
int[] two = goal [2];
您應該寫-
int[] zero = goal[0];
int[] one = goal[1];
int[] two = goal[2];
希望這可以幫助!
您的想法是正確的,但是您的實現是錯誤的。 嘗試:
int[] zero = goal[0];
您可以獲得單個數組的任何值,目標可以是任何維度。
int[][] goal = {{1, 2, 3}, {4, 5, 6}, {0, 7, 8} };
for(int i=0; i<goal.length; i++){
int [] actualArray = goal[i];
for(int j =0; j< actualArray.length; j++){
// you can get each value of individual array with the following code: actualArray[j];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.