求教,foreach如何赋值给数组
1个回答
展开全部
///具体实例如下:
package com.zte;
public class ForEachDemo {
public static void copy(int[] dist,int[] src) {
int j = 0;
for (int i : src) {
dist[j++] = i;
}
}
public static void printAry(int[] ary) {
for (int i = 0; i < ary.length; i++) {
System.out.print(" " + ary[i]);
}
}
public static void main(String[] args) {
int[] src = new int[] {1, 3, 4, 5, 6, 7, 10, 2};
int[] dist = new int[] {0, 0, 0, 0, 0, 0, 0, 0};
System.out.println("Before Copy:");
printAry(dist);
copy(dist ,src);
System.out.println("\nAfter Copy:");
printAry(dist);
}
}
package com.zte;
public class ForEachDemo {
public static void copy(int[] dist,int[] src) {
int j = 0;
for (int i : src) {
dist[j++] = i;
}
}
public static void printAry(int[] ary) {
for (int i = 0; i < ary.length; i++) {
System.out.print(" " + ary[i]);
}
}
public static void main(String[] args) {
int[] src = new int[] {1, 3, 4, 5, 6, 7, 10, 2};
int[] dist = new int[] {0, 0, 0, 0, 0, 0, 0, 0};
System.out.println("Before Copy:");
printAry(dist);
copy(dist ,src);
System.out.println("\nAfter Copy:");
printAry(dist);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询