我必须输入一个数组的长度,如何按正常顺序打印数字然后反转它!
示例:
输入数组的长度:3
输入int 1: 56
输入int 2: 10
输入int 3: 20
正常
56个
10个
20个
反向
20个
10个
56
import java.util.Scanner;
{
public static void main(String[] args)
{
int ArrayL;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length of the array: ");
ArrayLh= keyboard.nextInt();
// Create an array
double[] array = new double[ArrayL];
for (int index = 0; index < ArrayL; index++)
{
System.out.print("Enter Int" +
(index + 1) + ": ");
array[index] = keyboard.nextDouble();
}
}
}
答案 0 :(得分:1)
for(int i=0; i<ArrayLength;i++){
System.out.println(array[i]);
}
for(int i=ArrayLength-1; i>=0; i--){
System.out.println(array[i]);
}
答案 1 :(得分:0)
在Java 8中,您可以这样做:
List<Double> list1 = Arrays.stream(arrays).boxed().collect(Collectors.toList());
System.out.println(list1);
List<Double> list2 = new ArrayList<Double>(list1);
Collections.reverse(list2);
System.out.println(list2);
如果您需要使用空格连接元素,请使用:
String forwardText = list1.stream().map(Object::toString).collect(Collectors.joining(" "));
System.out.println(forwardText);
String backwardText = list2.stream().map(Object::toString).collect(Collectors.joining(" "));
System.out.println(backwardText);
答案 2 :(得分:0)
打印正常订单:
Arrays.stream(array).forEach(System.out::println);
打印逆序:
Arrays.stream(array).boxed()
.collect(Collectors.toCollection(ArrayDeque::new))
.descendingIterator().forEachRemaining(System.out::println);