遍历输出不同类型数组
大学课堂上老师布置的小练习,感觉挺有意思的,记录一下。
自己使用三个方法实现的。
package test02;
public class test023 {
// 方法一
public static void main(String[] args) {
int[] int_array = new int[] { 1, 2, 3 };
for (int num : int_array) {
System.out.print(num + "~");
}
System.out.println();
float[] float_array = new float[] { 1.0F, 2.0F, 3.0F, 4.0F };
for (float num1 : float_array) {
System.out.print(num1 + "~");
}
System.out.println();
double[] double_array = new double[] { 11.0D, 22.0D, 33.0D, 11.0D, 22.0D, };
for (double num2 : double_array) {
System.out.print(num2 + "~");
}
System.out.println();
//// 方法二
int len1 = int_array.length; // 3
int len2 = float_array.length; // 4
int len3 = double_array.length; // 5
Object[] fullArray = new Object[len1 + len2 + len3]; // 12
for (int i = 0; i < len1; i++) {
fullArray[i] = int_array[i];
// 0 0
// 1 1
// 2 2
}
for (int i = len1; i < len1 + len2; i++) {
fullArray[i] = float_array[i - len1];
// 3 0
// 4 1
// 5 2
// 6 3
}
for (int i = len1 + len2; i < len1 + len2 + len3; i++) {
fullArray[i] = double_array[i - len1 - len2];
// 7 0
// 8 1
// 9 2
// 10 3
// 11 4
}
for (Object num3 : fullArray) {
System.out.print(num3 + "~");
}
System.out.println();
// 方法三
showArray(int_array, float_array, double_array);
}
public static void showArray(int[] int_array, float[] float_array, double[] double_array) {
int len1 = int_array.length; // 3
int len2 = float_array.length; // 4
int len3 = double_array.length; // 5
Object[] fullArray = new Object[len1 + len2 + len3]; // 12
for (int i = 0; i < len1; i++) {
fullArray[i] = int_array[i];
// 0 0
// 1 1
// 2 2
}
for (int i = len1; i < len1 + len2; i++) {
fullArray[i] = float_array[i - len1];
// 3 0
// 4 1
// 5 2
// 6 3
}
for (int i = len1 + len2; i < len1 + len2 + len3; i++) {
fullArray[i] = double_array[i - len1 - len2];
// 7 0
// 8 1
// 9 2
// 10 3
// 11 4
}
for (Object num3 : fullArray) {
System.out.print(num3 + "~");
}
}
}
01.遍历输出不同类型数组