Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
How to cross iterate two arrayList in Java?

I have two arrayLists for now, and I want to do cross-iterations of them (basically, first iterate the 1st element in the first arrayList, and then iterate the 1st element in the second arrayList, next iterate the 2nd element in the first arrayList etc.). I am wondering how should I achieve this functionality?

Here is a simple test code:

public static void main(String[] args) {

        ArrayList first = new ArrayList();

        first.add("a");
        first.add("c");
        first.add("e");

        ArrayList second = new ArrayList();

        second.add("b");
        second.add("d");

        System.out.println(first);
        System.out.println(second);

    }

So basically, I would like to first extract the String a and do some coding, then String b, then String c, then d and e (note that the number of elements in the second arrayList is always one smaller then the first one).

Any hints will be much appreciated :)

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can iterate both lists from 0 until to second.size(), as you said second is always smaller.

And with a second for-loop iterate the rest elements from second.size() until to first.size()

ArrayList<String> first = new ArrayList<>();
first.add("a"); first.add("c");
first.add("e"); first.add("f");
first.add("g");

ArrayList<String> second = new ArrayList<>();
second.add("b");second.add("d");

for(int i = 0; i < second.size(); i++){
    System.out.println(first.get(i).toUpperCase());
    System.out.println(second.get(i).toLowerCase());
}

//iterate the rest elements remained in first
for(int i = second.size(); i < first.size(); i++){
    System.out.println(first.get(i).toUpperCase());
}
over 4 years ago · Santiago Trujillo Denunciar

0

Based on the explanation given in one of the comments, the List first should be renamed to actions and the List second should be moves. Further there is always one final action after the last move, and the processing always starts with an action.

Try this (but replace the '?' by something useful first):

public final void process( final List<?> a, final List<?> m )
{
  if( a.size() != m.size() + 1 ) throw IllegalArgumentException( … );
  final Queue<?> actions = new LinkedList<>( a );
  final Queue<?> moves = new LinkedList<>( m );
  
  while( !moves.isEmpty() )
  { 
    final var action = actions.remove();
    final var move = moves.remove();

    doAction( action );
    doMove( move );
  }
  final var action = actions.remove();
  doAction( action );
  
  assert actions.isEmpty();
  assert moves.isEmpty(); 
}

Call this as

process( first, second );

Of course you can avoid the the local variables by using the return value from Queue.remove() as the argument to the do…() methods.

over 4 years ago · Santiago Trujillo Denunciar

0

You could use a for loop with two counters and iterate from 0 to first.length

counterForFirst ( i in the foor loop ) = 0; counterForSecond = 0;

inside the loop, you would increment counterForSecond as well.

To avoid ArrayIndexOutOfBoundsException you could check if(counterForSecond < second.length ) in every itiration.

for (int counterForFirst = 0; counterForFirst < first.size(); counterForFirst++) {

         // action on first[counterForFirst]

         if(counterForFirst < seconds.length)
         {      
            // action on seconds 

              counterForSecond ++ ;
         }

      }     

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda