There are two ways to do this:
- A
forloop - Using the
iteratormethod.
for loop:
for(x currentX : GetList()) {
// Do something with the value
}
This is what's called a "for-each" loop, and it's probably the most common/preferred method of doing this. The syntax is:
for(ObjectType variableName : InCollection)
You could also use a standard for loop:
ArrayList<x> list = GetList();
for(int i=0; i<list.size(); i++) {
x currentX = list.get(i);
// Do something with the value
}
The syntax for this is:
for(someStartingValue; doSomethingWithStartingValue; conditionToStopLooping)
iterator method:
Iterator<x> iterator = GetList().iterator();
while(iterator.hasNext()) {
x currentX = iterator.next();
// Do something with the value
}
Answer from Caleb Brinkman on Stack OverflowThere are two ways to do this:
- A
forloop - Using the
iteratormethod.
for loop:
for(x currentX : GetList()) {
// Do something with the value
}
This is what's called a "for-each" loop, and it's probably the most common/preferred method of doing this. The syntax is:
for(ObjectType variableName : InCollection)
You could also use a standard for loop:
ArrayList<x> list = GetList();
for(int i=0; i<list.size(); i++) {
x currentX = list.get(i);
// Do something with the value
}
The syntax for this is:
for(someStartingValue; doSomethingWithStartingValue; conditionToStopLooping)
iterator method:
Iterator<x> iterator = GetList().iterator();
while(iterator.hasNext()) {
x currentX = iterator.next();
// Do something with the value
}
You can loop through your array with a for-each loop:
for (x item: GetList()) {
doSomethingWithEachValue(item);
}
for (String area : area_IdList)
{
for (String duplicatedArea : area_IdListduplicate)
{
if (!area.equalsIgnoreCase(duplicatedArea))
{
// some decision
}
}
}
This is more efficient, goes faster instead of iterating by indexes.
So this will check step by step each element in area_IdList with all elements in area_idListduplicate and each time they don't mach, this decision will be made. (If that's what you want achieve)
This is a O(2N) solution at some cost of 2N memory instead of N^2 time and N memory cost. Depends on the number of items that you have, but this solution will cost significantly less than the N^2 solutions.
Set<String> list=new Set<String>();
for (String area : area_IdList)
{
list.add(area.toLowerCase());
}
for (String duplicatedArea : area_IdListduplicate)
{
if(list.contains(duplicatedArea.toLowerCase())){
//Do something
}
}
As for not using index, see Use Enhanced For Loop Syntax
Videos
Assuming list is your List<String> retuned from the function. You may loop over it like:
for (int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
For assigning the EditText, you can just use the index, if you the number of items and it is fixed( which seem to be 5 here):
phoneNumber1.setText(list.get(0));
phoneNumber2.setText(list.get(1));
//so on ...
KOTLIN:
val numbers = listOf("one", "two", "three", "four")
for (item in numbers) {
println(item)
}
or
val numbers = listOf("one", "two", "three", "four")
val numbersIterator = numbers.iterator()
while (numbersIterator.hasNext()) {
println(numbersIterator.next())
}
stationNames here is not an array , but an ArrayList. So, you cannot use .length on it, use .size() :
Even simpler to do :
return stationNames.indexOf(stationName);
if it is found in the list will return its position or -1 if not.
Without all the hazzle of try catches and for loops. I could do the same task with one line of code, like following...
int output = stationIDs.get(stationNames.indexOf(stationName));
Thank you all for your help!
You want to follow the same pattern as before:
for (Type curInstance: CollectionOf<Type>) {
// use currInstance
}
In this case it would be:
for (Bullet bullet : gunList.get(2).getBullet()) {
System.out.println(bullet);
}
Edit:
Well, he edited his post.
If an Object inherits Iterable, you are given the ability to use the for-each loop as such:
for(Object object : objectListVar) {
//code here
}
So in your case, if you wanted to update your Guns and their Bullets:
for(Gun g : guns) {
//invoke any methods of each gun
ArrayList<Bullet> bullets = g.getBullets()
for(Bullet b : bullets) {
System.out.println("X: " + b.getX() + ", Y: " + b.getY());
//update, check for collisions, etc
}
}
First get your third Gun object:
Gun g = gunList.get(2);
Then iterate over the third gun's bullets:
ArrayList<Bullet> bullets = g.getBullets();
for(Bullet b : bullets) {
//necessary code here
}
public void FunctionExample(ArrayList contacts) {
for(int i=0; i < contacts.size(); i++) {
LinkedTreeMap<String, Object> map = (LinkedTreeMap<String, Object>) contacts.get(i);
map.containsKey("id");
String id = (String) map.get("id");
map.containsKey("contact_name");
String contact_name = (String) map.get("contact_name");
map.containsKey("numbers");
String numbers = (String) map.get("numbers");
numbers.replace("{","").replace("}","");
map.containsKey("emails");
String emails = (String) map.get("emails");
emails.replace("{","").replace("}","");
Snackbar.make(getView(), id, Snackbar.LENGTH_LONG).show();
Snackbar.make(getView(), contact_name, Snackbar.LENGTH_LONG).show();
Snackbar.make(getView(), numbers, Snackbar.LENGTH_LONG).show();
Snackbar.make(getView(), emails, Snackbar.LENGTH_LONG).show();
}
}
Try this..It will give arrayList of id's
JSONObject object=new JSONObject(response);
JSONArray array= null;
try {
array = object.getJSONArray("response");
} catch (JSONException e) {
e.printStackTrace();
}
ArrayList<String> idArray=new ArrayList<>();
for(int i=0;i< array.length();i++)
{
idArray.add(getJSONObject(i).getString("id"));
}
When you use myList.getAdapter().getView(i,null,null) you are getting a new instance of the item view, try the ListView.getChildAt(position) method like this:
private void ButtonClick() {
/** get all values of the EditText-Fields */
View v;
ArrayList<String> mannschaftsnamen = new ArrayList<String>();
EditText et;
for (int i = 0; i < myList.getCount(); i++) {
v = myList.getChildAt(i);
et = (EditText) v.findViewById(R.id.editText1);
mannschaftsnamen.add(et.getText().toString());
}
....
}
Solved the Problem with the help of a friend:
private void ButtonClick() {
/** get all values of the EditText-Fields */
View v;
ArrayList<String> mannschaftsnamen = new ArrayList<String>();
EditText et;
for (int i = 0; i < myList.getCount(); i++) {
v = myList.getAdapter().getView(i, null, null);
et = (EditText) v.findViewById(i);
mannschaftsnamen.add(et.getText().toString());
}
....
}