如何在不使用循环结构的情况下遍历ArrayList?

Mar*_*vam 3 java

在Java中如何在不使用任何循环结构的情况下遍历ArrayList?

Ree*_*ore 8

你可以使用递归.

public void doSomethingToAll(List list)
{
    // Begin the recursion.
    doSomethingToAll(list, 0);
}

private void doSomethingToAll(List list, int index)
{
    // Break the recursion when we have processed the entire list.
    if (index >= list.size()) return;

    // Do whatever you want with the list.
    process(list.get(index));

    // Recursive step. Process the next element.
    doSomethingToAll(list, index + 1);
}
Run Code Online (Sandbox Code Playgroud)