public static void main(String[] args) throws Exception { int[][] array = new int[][] { new int[] {1,2,3}, new int[] {11,22,33,44}, new int[] {111,222,333,444,555} }; int[] newArray = null; int totalLength = 0; int copied = 0; int part1 = array.length; int part2 = array.length << 1; for (int i = 0; ; i++) { if (i < part1) { // PART I: calculate totalLength; totalLength += array[i].length; continue; } if (i == part1) { // PART II-a: initialization on the new array newArray = new int[totalLength]; } if (i < part2) { // PART II-b: clone int index = i - array.length; System.arraycopy(array[index], 0, newArray, copied, array[index].length); copied += array[index].length; continue; } if (i < part2 + totalLength) { // PART III: print System.out.println(newArray[i - part2]); continue; } if (i == part2 + totalLength) { // Loop end break; } } }
int[] is not instanceof Object[],否则jdk的Arrays就不用写那么overload了
想得太复杂了吧,一个while好了 int[][] a = {{1,2,3}, {4,5,6}}; int i=0,j=0; while (i<a.length) { System.out.println(a[i][j]); j++; if (j>=a[i].length) { i++; j=0; } }
小菜也山寨了一种: public class Xiaocai { public static void main(String[] args) { int[][] a = new int[][]{{1,2,3},{11,22,33},{111,222,333},{555555}}; int i, j; for (i = 0,j = 0; j <= a[i].length; j ++) { if (j == a[i].length) { j = -1; i ++; if (i == a.length) { break; } continue; } System.out.println(a[i][j]); } } }感谢大家。
把while改成for也一样的 int[][] a = {{1,2,3}, {4,5,6}}; for (i=0,j=0;i<a.length;) { System.out.println(a[i][j]); j++; if (j>=a[i].length) { i++; j=0; } }
int[][] array = new int[][] {
new int[] {1,2,3},
new int[] {11,22,33,44},
new int[] {111,222,333,444,555}
};
int[] newArray = null;
int totalLength = 0;
int copied = 0;
int part1 = array.length;
int part2 = array.length << 1;
for (int i = 0; ; i++) {
if (i < part1) {
// PART I: calculate totalLength;
totalLength += array[i].length;
continue;
}
if (i == part1) {
// PART II-a: initialization on the new array
newArray = new int[totalLength];
}
if (i < part2) {
// PART II-b: clone
int index = i - array.length;
System.arraycopy(array[index], 0, newArray, copied, array[index].length);
copied += array[index].length;
continue;
}
if (i < part2 + totalLength) {
// PART III: print
System.out.println(newArray[i - part2]);
continue;
}
if (i == part2 + totalLength) {
// Loop end
break;
}
}
}
System.arraycopy是native的,API里面没循环
public static void main(String[] args) {
String[][] str = new String[2][2];
str[0][0] ="00";
str[0][1] = "01";
str[1][0] = "10";
str[1][1] = "11";
printArr(str);
}
public static void printArr(Object[] arr){
for(int i = 0; i<arr.length;i++){
Object obj = arr[i];
if(obj instanceof Object[]){
printArr((Object[])obj);
}else{
System.out.print(arr[i] + "\t");
}
}
System.out.println();
}
int[][],你倒是打印啊
int[][] arr = { { 123, 465, 465, 796 }, { 123, 123, 465, 65465, 142 }, { 123, 123, 465, 65465, 142 }, { 123, 123, 465, 65465, 142 }, { 123, 123, 465, 65465, 142 }, { 123, 123, 465, 65465, 142 } }; for (int[] a : arr) {
print(a, 0);
System.out.println("\r\n");
}
} public static void print(int[] arr, int i) {
if (i < arr.length) {
System.out.print(arr[i] + " ");
print(arr, i + 1);
}
}
int[][] a = {{1,2,3}, {4,5,6}};
int i=0,j=0;
while (i<a.length) {
System.out.println(a[i][j]);
j++;
if (j>=a[i].length) {
i++;
j=0;
}
}
public class Xiaocai {
public static void main(String[] args) {
int[][] a = new int[][]{{1,2,3},{11,22,33},{111,222,333},{555555}};
int i, j;
for (i = 0,j = 0; j <= a[i].length; j ++) {
if (j == a[i].length) {
j = -1;
i ++;
if (i == a.length) {
break;
}
continue;
}
System.out.println(a[i][j]);
}
}
}感谢大家。
int[][] a = {{1,2,3}, {4,5,6}};
for (i=0,j=0;i<a.length;) {
System.out.println(a[i][j]);
j++;
if (j>=a[i].length) {
i++;
j=0;
}
}