[英]Java integer list
I am trying to make java go trough a list of numbers. 我试图让java通过一个数字列表。 It chooses the first one, gives this as output, waits/sleeps like 2000 milliseconds and then give the next one as output, waits 2000 milliseconds, etc. 它选择第一个,将其作为输出,等待/休眠,如2000毫秒,然后将下一个作为输出,等待2000毫秒等。
They should not be listed behind eachother so when my list is: 10 20 30 40 50 它们不应该列在彼此之后,所以当我的列表是:10 20 30 40 50
It should not give as output: 10 20 30 40 50. But just 50. 它不应该作为输出:10 20 30 40 50.但只有50。
It would be even better if it was able to repeat itself. 如果它能够重演它会更好。
So far i tried: 到目前为止,我试过:
List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
Iterator<Integer> myListIterator = someList.iterator();
while (myListIterator.hasNext()) {
Integer coord = myListIterator.next();
}
But this has no timer, and i am not sure if this will only show '20' or '10 20 30 40 50' as output. 但这没有计时器,我不确定这只会显示'20'或'10 20 30 40 50'作为输出。 And i dont really know how to put a sleep/wait command and a repeat command in this :s (might have overlooked the repeat command if its already in) 我真的不知道如何在这个中放入一个sleep / wait命令和一个repeat命令:s(如果它已经存在,可能忽略了repeat命令)
Edit Tyvm all, i can go on with the rest of the coding now :) 编辑 Tyvm all,我现在可以继续使用其余的编码了:)
If you want to rewrite a line on console, print a control character \\r
(carriage return). 如果要在控制台上重写一行,则打印一个控制字符\\r
(回车)。
List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
Iterator<Integer> myListIterator = myCoords.iterator();
while (myListIterator.hasNext()) {
Integer coord = myListIterator.next();
System.out.print("\r");
System.out.print(coord);
Thread.sleep(2000);
}
code that works, but output is: 有效的代码,但输出是:
10
20
30
40
50
so: 所以:
List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
for (Integer number : myCoords) {
System.out.println(number);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
To insert a sleep command you can use Thread.sleep(2000). 要插入sleep命令,可以使用Thread.sleep(2000)。 So the code would be: 所以代码是:
List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
Iterator<Integer> myListIterator = someList.iterator();
while (myListIterator.hasNext()) {
Integer coord = myListIterator.next();
System.out.println(coord);
Thread.Sleep(2000);
}
This would output: 10 20 30 40 50 这将输出:10 20 30 40 50
If you want the numbers after each other you could use: System.out.print(coord +" " ); 如果你想要彼此之后的数字,你可以使用:System.out.print(coord +“”); and if you want to repeat the section you can put it in another while loop. 如果你想重复这个部分你可以把它放在另一个while循环中。
List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
while(true)
Iterator<Integer> myListIterator = someList.iterator();
while (myListIterator.hasNext()) {
Integer coord = myListIterator.next();
System.out.print(coord + " ");
Thread.Sleep(2000);
}
}
This would output: 10 20 30 40 50 10 20 30 40 50 ... and never stop until you kill the program. 这将输出:10 20 30 40 50 10 20 30 40 50 ...并且在你杀死程序之前永远不会停止。
Edit: You do have to put the sleep command in a try catch block 编辑:您必须将sleep命令放在try catch块中
So it would become: 所以它会成为:
List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
while(true)
Iterator<Integer> myListIterator = myCoords.iterator();
while (myListIterator.hasNext()) {
Integer coord = myListIterator.next();
System.out.print("\r" + coord);
try{
Thread.sleep(2000);
}catch(Exception e){
// handle the exception...
}
}
}
Let's use some java 8 feature: 让我们使用一些java 8功能:
IntStream.iterate(10, x -> x + 10).limit(5)
.forEach(System.out::println);
If you need to store the numbers you can collect them into a collection eg: 如果您需要存储数字,可以将它们收集到一个集合中,例如:
List numbers = IntStream.iterate(10, x -> x + 10).limit(5)
.boxed()
.collect(Collectors.toList());
And some delay added: 还有一些延迟:
IntStream.iterate(10, x -> x + 10).limit(5)
.forEach(x -> {
System.out.println(x);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// Do something with the exception
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.