How to traverse an ArrayList without using loop loops?

In Java, how to navigate an ArrayList without using any construction loops?

+3
source share
4 answers

You can use recursion.

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);
}
+7
source

Not to mention what I propose, this is the only and best solution, but it is general enough to be reusable, safe for text and allowing you to break early during your crawl.

You can easily expand it and make it common to any type of collection.


Simple trace implementation

The implementation consists of:

  • a reusable class of type Actor for defining actions (if any),
  • reusable safety class,
  • , .

, , , .

Actor

interface Actor<T> {

  void act(final T element) throws TraversalInterruptionException;
}

Traversal

class Traversal<T> {

  int      index = 0;
  Actor<T> actor = null;


  public Traversal(final Actor<T> a) {
    actor = a;
  }

  void traverse(final List<T> col) {
    if (index < col.size()) {
      final T element = col.get(index++);
      try {
        if (actor != null) {
          actor.act(element);
        }
        traverse(col);
      } catch (final TraversalInterruptionException e) {
        /* let get out of there early */
        index = 0; // reset
      }
    } else {
      index = 0; // reset
    }
  }
}

TraversalInterruptionException

class TraversalInterruptionException extends Exception {
}

  @Test
  public void test() {
    final List<String>      list          = new ArrayList<String>();
    final Traversal<String> strTraversal  = new Traversal<String>(new Actor<String>() {

      @Override
      public void act(final String element)
      throws TraversalInterruptionException {
        if ("exit".equals(element)) {
          throw new TraversalInterruptionException();
        }
        System.out.println(element);
      }
    });

    list.add("test1");
    list.add("test2");
    list.add("test3");
    list.add("exit");
    list.add("test4");

    strTraversal.traverse(list);
    strTraversal.traverse(list); // and again to test the reset
  }

test1
test2
test3
test1
test2
test3
+1

You can do something dumb:

void traverse(ArrayList list)
{
  traverseHelper(list, 0);
}

void traverseHelper(ArrayList list, int index)
{
  if (index == list.size())
    return;

  "visit" list.get(index);

  traverseHelper(list, index+1);
}

It uses recursion instead of iteration, but why do you need to do this?

0
source

You can try with recursion:

void traverse(ArrayList<Whatever> list, int cur)
{
   if (cur == list.size())
     return;
   else {
     Whatever elem = list.getAt(cur);
     System.out.println(elem);
     traverse(list, ++cur);
   }
}

void traverse(ArrayList<Whatever> list)
{
  traverse(list, 0);
}
0
source

Source: https://habr.com/ru/post/1778512/


All Articles