Output must be 1 2 3 4 5 6 7 8 9 10 I have tried While loop but it prints from 10 to 1 Answer Assuming you must use i with an initial value of 10.
Tag: loops
ArrayIndexOutOfBoundsException when finding words in an array that and with a specific letter
I am trying to find words in an array that end with a letter ‘a’. I thought of doing it using two for loops but I keep getting integer out of bounds error. Could anyone tell me what i am doing wrong? The code: Answer You’ve got too much code for the task, which has lead to a bug creeping
Subtraction loops in java
I am new to java and am stuck on this challenge. I am to calculate the state of water depending on the inputed temp and altitude. However, for every 300 metres (for altitude) the boiling point is to drop by 1 degree. I am confused as to how to make it a loop that will take one off for every
(Java) How can I make a loop that will assign random names I’ve generated from an array to multiple strings?
Relatively new to Java, looking for a solution to a crucial part of a game I’m making for a class. The idea is to make a very simple stock market simulation game, but the problem is related to creating made-up company names. I have three arrays for the first, middle, and last names of companies. I’m trying to make some
Asynchronous execution/operation with CompletableFuture in Java 8+
In Java, is calling get() method while looping on CompletableFuture instances as good as doing synchronous operations although CompletableFuture is used for async calls? Answer ‘get()’ waits until the future is completed. If that’s what you want, it’s what you use. There’s no general rule. For example, you might be using a method that is inherently asynchronous, but in your
No ConcurrentModificationException during index based iteration
I have following code: I expected it to throw ConcurrentModificationException but it is working fine. Can some explain the reason? Answer The reason is you are not technically iterating the List. Instead you are random accessing the list using a incrementing index, and removing some values. If you change to code like this to iterate the list it will throw
Is there a way to go back by one iteration in a for-each loop?
For example, if I were to do… If it isn’t possible, is there another method of traversing through some sort of collection class while controlling the iteration counter? I tried looking through the answers to this question but couldn’t think of a way that was clear to me. Any suggestions are appreciated! Answer It depends on what you are trying
creating multiple threads with a unique id (threads do not overlap)
UPDATE I’m solving a producer / consumer problem, and I want to create a few producers and consumers (several threads) and I have a question how can I create several threads correctly so that one task is not performed by two threads (each thread does a different task). code: I tried to do it just in a loop like here:
What is the quickest way to determine if two elements are in the same row or column in a 2D array?
As part of one of my programs, I’m trying to check if two elements in a 2D array are part of the same row or column. I know there might be many ways to go about this, mainly with loops and by iterating through the array, but I was just wondering: is there any quick way to determine this? Let’s
How do I work with objects in lists and can I create multiple objects with different names in a loop?
I need to create Objects of a class called “Tile”. I have 9×9 chess board like grid and want to initiate the whole board at once. The position of a Tile is defined by an x and y-axis and I …