我有一个数组[10]将我的数组分成两个不同的数组A和B. 具有偶数索引和B的数组元素 奇数索引数组的元素。
使用for循环以适当的代码快速响应。
答案 0 :(得分:2)
class Main {
public static void main(String args[]) {
// Creating arrays
int array[], a[], b[];
array = new int[10];
a = new int[5];
b = new int[5];
// Giving initial values to array, and printing
System.out.print("array: " );
for (int i = 0; i < array.length; i++) {
array[i] = (int) (Math.random()*10);
System.out.print("\t" + array[i]);
}
System.out.println();
// Splitting the array in two
for (int i = 0; i < 5; i++) {
a[i] = array[2 * i];
b[i] = array[2 * i + 1];
}
// Printing a
System.out.print("a : " );
for (int i = 0; i < a.length; i++) {
System.out.print("\t" + a[i]);
}
System.out.println();
// Printing b
System.out.print("b : " );
for (int i = 0; i < b.length; i++) {
System.out.print("\t" + b[i]);
}
System.out.println();
}
}
输出:
array: 8 8 6 0 4 0 0 9 9 2
a : 8 6 4 0 9
b : 8 0 0 9 2