Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java integer list

Tags:

I am trying to make java go trough a list of numbers. 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.

They should not be listed behind eachother so when my list is: 10 20 30 40 50

It should not give as output: 10 20 30 40 50. But just 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. 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)

Edit Tyvm all, i can go on with the rest of the coding now :)

like image 511
JJ90 Avatar asked Mar 20 '11 21:03

JJ90


People also ask

Can you have a list of ints in Java?

Create List of Ints Using the MutableIntList Class in Java. If you are working with the eclipse collection library, then use the MutableIntList class to create a list of ints. Here, we used the empty() method to create an empty list and then the add() method to add elements.

How do you add integers to a list in Java?

Using Stream.collect() The second method for calculating the sum of a list of integers is by using the collect() terminal operation: List<Integer> integers = Arrays. asList(1, 2, 3, 4, 5); Integer sum = integers.

What is list of () method in Java?

The list() method of java. util. Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that require collections.


2 Answers

If you want to rewrite a line on console, print a control character \r (carriage return).

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); } 
like image 65
Messa Avatar answered Sep 27 '22 20:09

Messa


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();         }     } 
like image 23
dantuch Avatar answered Sep 27 '22 20:09

dantuch