Foreach Loop in java [closed] - java

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
for(int i:arr)
arr[i]=sc.nextInt();
Can we accept with this syntax?
I am trying to accept element using for-each loop in Java and while printing the value it is giving garbage value

This won't work.
for(int i:arr)
arr[i]=sc.nextInt();
i has value of the array element, not the index value.
Or you need to initialise the array like and then set the value at the index:
for(int i = 0 ; i < len; i++)
arr[i] = i;
for(int i:arr)
arr[i]=sc.nextInt();

Iterators return you the values in the array rather than the index itself and foreach basically uses iterators.
You should instead use a normal loop for this as shown:
for(int x=0;x<arr.length;x++){
arr[x]=sc.nextInt();
}

No, you can't use this syntax as this will only update the zero index in the array and will be updated by the last input value from the user in the 0th index of the array.
Let's say you have a 3 sized array and you give 3 2 1 as input from the user then only the first index of the array will be updated and at last, it will be containing the last input by the user. So finally the array will be [1 0 0] instead of [3 2 1] which is actually desired.
So to overcome this issue you have to use the normal for loop.

Related

how to optimise two for loops in java [closed]

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.

String comparison from a list in for loop [closed]

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 3 years ago.
Improve this question
I'm trying to compare strings from a list in a for loop but I can't really find the syntax for it. I want to change the iterator (in this case i) to not be the index, instead, I want it it to be the value.
list.add(lrow);
for (int i = 0; i < list.size(); i++) {
if (lrow.equals(i)){
}
}
handler.getGalagaState().entityManager.entities.add(new EnemyBee(0 , 0, 32 , 32, handler, row, column));
You have to get value from ArrayList by index. You can do it like this:
list.get(i);
I want to change the iterator (in this case i) to not be the index, instead, I want it it to be the value.
'i' is not the iterator. Its just a variable of type integer that let's you setup a condition for iterating through a Collection.
With that being said, how would you implement the condition-check for continuing or exiting the loop when the control-variable is of type String?
Your comparison should look like this:
if (lrow.equals(list.get(i)))
list.get(i) gets you the element at index i in your List.

(Java) Go thru array beginning at the last index [closed]

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

How to set values in a 2D array? (Java) [closed]

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
I'm making a method that takes 2 inputs, one is int[][], the other is int. I need to store that int value as the "row" of the int array. For example if I input (int[][] array, int 3493), I need the row to be {3 4 9 3}. This is basically like the 2048 games, as I need to be able to manipulate the array later. I do not know the proper syntax for this, however. Please help.
Thank you.
As An Sample, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional
arrays and each contains four elements, as shown in the figure x.length is 3, and
x[0].length, x[1].length, and x[2].length are 4
As you see you need two for loops to go through the array.
1. for traversing the row one by one
2. when you are in each row traversing each column one by one
for example:

How to counter the number of strings exceeding a certain number of characters in a string array. Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I get how to count the number of words in a string by extinguishing the spaces between words, but am confused of how to approach it when it asks for strings equal to or exceed a certain number of characters.
Complete the countLongWords() method below, which takes an
array of Strings as its argument. This method returns an integer value
representing the number of Strings in the array that are 5 or more characters
long.
pseudocode
int count = 0
for str in stringArray
if str.length() >=5
count++
return count
You can convert this to java
Try to always think what you will do if you have a paper with this array of Strings written on it and want to count the number of Strings that satisfied this property.
Then it's very easy to translate that in a computer program.
Create a variable to hold the number of Strings with a length superior than 5
Loop through the element of your array
For each element, if it holds the property increment this variable
Return the variable
int number = 0;
for(int i = 0; i<array.length; i++){
if(array[i].length()>4){
number++;
}
}
return number;

Categories