package practice;
public class ArrayElementsAddIndices {
public static void main(String[] args) {
int array[] = { 5, 4, 3, 2, 1, 6, 7, 8, 9 };
System.out.println("原数组中的元素:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("\n\n数组元素选择排序:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println("\n\n数组元素及其对应下标:");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i] + " —— " + i);
}
int newArray[] = new int[array.length];
System.out.println("\n原数组元素加上对应下标后生成的结果元素:");
for (int i = 0; i < newArray.length; i++) {
System.out.print((newArray[i] = array[i] + i) + " ");
}
}
}
Java输出数组元素与其对应下标之和
最新推荐文章于 2022-10-25 15:01:02 发布