二维数组如何一个for循环打印出所有的元素。

daijope 2011-08-09 11:44:38
RT,for while什么的只能用一次,也不能有Java内置的API吧,,,
...全文
575 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl3450341 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 shine333 的回复:]

int[] is not instanceof Object[],否则jdk的Arrays就不用写那么overload了
[/Quote]

我的代码,只是提供一种思路。。。。你看小绵羊不是帮我改的很好了?
shine333 2011-08-09
  • 打赏
  • 举报
回复
int[] is not instanceof Object[],否则jdk的Arrays就不用写那么overload了
小绵羊 2011-08-09
  • 打赏
  • 举报
回复
递归如何??这可只有一个for哦

public static void main(String[] args) {
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);
}
}
zl3450341 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 shine333 的回复:]

引用 8 楼 zl3450341 的回复:

引用 5 楼 shine333 的回复:

可以的,给我满分吧
System.arraycopy是native的,API里面没循环


你的写的太复杂了。。还满分。。看下我的。。

递归是耍赖,而且,你的程序不符合要求
int[][],你倒是打印啊
[/Quote]

有说不许用递归么?你可以用我的程序测试一下啊。。
shine333 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zl3450341 的回复:]

引用 5 楼 shine333 的回复:

可以的,给我满分吧
System.arraycopy是native的,API里面没循环


你的写的太复杂了。。还满分。。看下我的。。
[/Quote]
递归是耍赖,而且,你的程序不符合要求
int[][],你倒是打印啊
zl3450341 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 shine333 的回复:]

可以的,给我满分吧
System.arraycopy是native的,API里面没循环
[/Quote]

你的写的太复杂了。。还满分。。看下我的。。
shine333 2011-08-09
  • 打赏
  • 举报
回复
上次小绵羊那个题目,我已经查过无数API,清楚哪些用了循环,哪些没有...
zl3450341 2011-08-09
  • 打赏
  • 举报
回复

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();
}

shine333 2011-08-09
  • 打赏
  • 举报
回复
可以的,给我满分吧
System.arraycopy是native的,API里面没循环
shine333 2011-08-09
  • 打赏
  • 举报
回复
  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;
}
}
}
daijope 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zl3450341 的回复:]

最近很流行玩智力题哦。
[/Quote]
我是听一个老师在吹,要考考我们,能不能呢?
zl3450341 2011-08-09
  • 打赏
  • 举报
回复
最近很流行玩智力题哦。
shine333 2011-08-09
  • 打赏
  • 举报
回复
智力题目?
小绵羊 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 qybao 的回复:]

引用 15 楼 qybao 的回复:
想得太复杂了吧,一个while好了

Java code

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) {
……
[/Quote]
daijope 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 qybao 的回复:]

引用 15 楼 qybao 的回复:
想得太复杂了吧,一个while好了

Java code

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) {
……
[/Quote]
不愧是阿宝,,
qybao 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 qybao 的回复:]
想得太复杂了吧,一个while好了

Java code

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;……
[/Quote]
把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;
}
}
daijope 2011-08-09
  • 打赏
  • 举报
回复
小菜也山寨了一种:
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]);
}
}
}

感谢大家。
qybao 2011-08-09
  • 打赏
  • 举报
回复
想得太复杂了吧,一个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;
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧