0

I'm wondering for the simplest method for how to run a specific function for each object in an array (or other list type)

My goal is to be able create a list of objects, and have each object run a specific function as it passes through the iterator.

I've tried a for loop on an arraylist

for (int i = 0; i < testList.size(); i++)
    {
        this  = textList.get(i);
        this.exampleFunction();
    }

But this gives me a 'Variable expected' error

19
  • A loop? (for loop) docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html Commented Jul 10, 2018 at 1:29
  • @GBlodgett I had accidentally submitted before finishing, I have tried a for loop Commented Jul 10, 2018 at 1:32
  • 3
    Foo foo = textList.get(i); foo.function(); Commented Jul 10, 2018 at 1:32
  • In what context is this? What is this.function? Commented Jul 10, 2018 at 1:33
  • this.function() ? Commented Jul 10, 2018 at 1:33

2 Answers 2

3

Assuming you're using Java 8+, and you have a Collection<TypeInList> you could call Collection.stream() and do a forEach on that. Like,

testList.stream().forEach(TypeInList::function);

Your current approach is trying to do things with this that cannot be done. It could be fixed like,

for (int i = 0; i < testList.size(); i++)
{
    TypeInList that = testList.get(i); // this is a reserved word.
    that.function();
}

or

for (TypeInList x : testList) {
    x.function();
}
Sign up to request clarification or add additional context in comments.

4 Comments

No need for stream(). Iterable (and therefore every Collection) has its own forEach method.
This compiles, but it crashes when I try to reach the part where this section executes.
Crashes in what way? Is there a stack trace? Some kind of error message?
@DawoodibnKareem I'm too inexperienced to know what a stack trace is, and no error message. I've actually narrowed it down, his example is fine, it's my own code that is faulty. How do I add an object to the list as part of the constructor? Using .add(this) is what was causing the crash.
0

There are multiple ways to iterate through a list, but the easiest I personally find is like this:

Assuming that your list contains String objects e.g.:

List<String> list = new ArrayList();
list.add("Hello");
list.add("World");

for(String current : list){
    System.out.println(current);
}

The loop will iterate twice, and console will output the following:

Hello
World

This approach doesn't rely on indexes (as how you're using it in your question), as such I find it easy to use for iterating through a single list.

However the disadvantage is that if you have 2 separate lists that you would like to iterate through, the lack of indexes makes it a bit more complicated. The easier approach for iterating through multiple lists would be using the traditional approach, something like this:

for(int i=0; i<list.size(); i++){
    int x = list1.get(i);
    int y = list2.get(i);
}

As such your use-case really determines the ideal method you can adopt.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.