String comparison from a list in for loop [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 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.

Related

Foreach Loop 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
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.

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.

How to get the last value of an element in a List in Java? [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 1 year ago.
Improve this question
Is it possible to get the last value of an element inside a list with many different values?
List<EventDTO> eventDto = new ArrayList<>();
Inside this list there are three elements: id(Integer), name(String), and lastStatus(String)
I'm trying to compare a String to see if it's equal to the last value stored in the element lastStatus inside the list. As the event is updated, many values are stored in said element. I'm trying to get the last one stored.
String originalStatus = 'Pending';
How can I access the list, find the element lastStatus, and see if the last value stored inside of it equals to originalStatus ?
In accordance to Java 8 here https://docs.oracle.com/javase/8/docs/api/java/util/List.html, you could use the function
E get(int index)
to access last element in a list. It returns the element at the specified position in this list.
Example: eventDto.get(eventDto.size() -1 )
while using the function
int size()
to get the last index of the list.
For comparison, you can use the function
boolean equals(Object o) of String class on the the adequate attribut.

(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

Is there any fast way to remove an element from a string array in Java? [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 7 years ago.
Improve this question
array = ArrayUtils.removeElement(array, array[i]);
This is working for int array, not for String array...
Is there any fast way to remove an element from a string array in Java?
Seems that you are using org.apache.commons.lang.ArrayUtils class, which is not standard Java class but part of Apache Commons Lang library. If you know an index of element, it would be more efficient to use
array = ArrayUtils.remove(array, i);
As this version will not search for given element, just remove by index. This should work for object arrays (including String array) as well.
Since array can not be resized, it may create some problems (eg.- raise of cost) when you try to remove some element from an array.
In this case ArrayList would be a better alternative. You can add/remove element from ArryList more continently -
List<String> strgs = new ArrayList<String>();
strgs.add("first");
strgs.add("second");
strgs.add("third");
strgs.remove("second");

Categories