Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm currently trying to make a method in Java that takes an array of size digits, and is supposed to go thru the array starting at the end, adding 1 to digits <= 8, and turning 9's into 0's.
My problem is that I'm not quite sure how to start going through the array beginning at the end. I'm coming off of Python, so I'm familiar with the syntax list[:-1], but I'm not sure how to apply that, or if it can be applied, to Java.
Thank you very much in advance.
Use this:
public void someMethod(int[] arr){
for(int i=arr.length-1; i >= 0; i--){
if(arr[i] <= 8){
arr[i]+=1;
}else if(arr[i] ==9){
arr[i] = 0;
}
}
}
Refer https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for better undersatnding of for loop construct.
Java has unfortunately no operation like list[:-1] so you will need to do a reversed for loop or a decremental lopp to achieve that:
for (int i = value; i > 0; i--) {
System.out.println("Am decreasing over the element: " + i);
}
Also, please take a look at the Oracle tutorial about for loops
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is there any way to optimise these for loops ?
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(i < j)
{
if(a[i]+a[j] >= l && a[i]+a[j] <= r)
count++;
}
}
}
It depends what these 2 loops do.
Imagine that these 2 loops are traversing a 2 dimensional table.
If you need to find the sum of all the elements of the table this cannot be optimised. You have to traverse all the elements line by line.
If you want to find a specific value of the AI for example... that you have to traverse again and again the same values, in this case you can optimize by not traversing the same values you did before.
For example if these values of the table are values of a tree structure, and you traverse a branch, after you don't have to traverse the same branch. You can prune it as we say.
With this way you optimize your searching in the table using the loops.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
https://gyazo.com/fd643045f8f8eb285b25142013095947
What can I add to this code, to replace all of the number 5 with 0?
Thanks!
Loop over the array, check if the number is 5, and if so set them to 0
for(int i = 0; i < arr.length; i++) {
if(arr[i] == 5) arr[i] = 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to find a way to implement an algorithm in Java that can calculate (a+b+...+n)^2 with a,b,n >= 0. The purpose is to use it afterwards in order to calculate Jain's Fairness index for my algorithm in networks. Is there any standard way to do that or any specific library for advanced math that i might have missed?
Just sum those n values in for loop and then multiply it by itself. Or am i missing something?
The number of possible problems you may encounter is infinite, so you should not be surprised if you often get into a situation where there is no method to help you. Let's suppose that you have an array of numbers, let's suppose it has double elements and the name of the array is input, then:
double sum = 0;
for (int index = 0; index < input.length; index++)
sum += input[index];
double result = Math.pow(sum, 2);
//output holds the result
If there is a possibility of overflow, then you will need to handle it. Also, you will need to handle the validation that your items are positive.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am currently trying to make a Mad Libs game, and all the words are stored in an ArrayList called List. Now, I want to display all the words within that list, but only 10 per line. Is there some formatting using %f or something that can solve this problem? I have looked far and wide on this site, but I haven't found anything.
Just use a loop. Iterate over the full list of words, and upon hitting every tenth word, print a newline separator. Otherwise, print a space separator between each word.
List<String> words;
for (int i=0; i < words.size(); ++i) {
if (i % 10 > 0) {
System.out.print(" ");
}
System.out.print(words.get(i));
if ((i+1) % 10 == 0) {
System.out.println(""); // print correct newline regardless of OS
}
}
Demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The Boolean array is initialized to true. I need to know how to turn every third value to false and also going through the array over and over. It is essentially duck duck goose without the randomness.
This sounds like a beginners programming exercise, so I'm going to just give you a hint or two:
Go back to your textbook, lecture notes, tutorial and reread the stuff on for loops. Focus on the old style ones.
Think about how to write a for loop that steps through integer index values in the pattern that you require.
Re "... going through the array over and over" - not sure what you mean, but maybe the hint for this is to think about using nested loops; i.e. a loop inside another loop.
But the most important advice is to try and work this out for yourself.
Well I'm really not sure what you mean by going through the array over and over but the following code will turn every third value to false.
for (int i = 0; i < myVar.length; i++) {
if (i % 3 == 0) {
myVar[i] = false;
}
}
Edit: Oops someone beat me to it while I was typing lol.