Touring java arrangement efficiently

6

Lately I wanted to start thinking more than solutions, to be effective at the time of programming. And that is why I run into the following question, and as the title says:

What is the most efficient way to traverse an array in Java?

And more than importing the method, it is the why it is the best way, since this way I can apply this knowledge in other different cases.

    
asked by José Miguel Sepulveda 13.04.2017 в 14:36
source

3 answers

5

A better way does not exist, but there are different ways for your use cases. As the size of an array does not change in Java after it has been created, the classic way is by an iteration over the indexes:

// ejemplo de arreglo
Object[] arreglo = new Object[10];
//...
for (int i = 0; i < arreglo.length; i++){
    // aqui se puede referir al objeto con arreglo[i];
}

This method uses a simple loop with a primitive like index so it is not very expensive, and it is used when it is necessary to know the index together with the object of the arrangement.

Another way, if there is no importance, how many elements exist in the array or no reference to the index is needed:

for (Object o : arreglo){
    // en cada iteración "o" se refiere a un objeto del arreglo para todos objetos en el arreglo
}

Both forms need few resources and we must remember that it does not make much sense in Java to over-think the optimization of code, because a tremendous intelligence was invested to optimize the execution of the bytecode in the virtual machine from the JIT-compiler (just in time).

Just remember that it is advisable in the performance context to avoid very high functions for simple cases if you want to minimize the head. The next example uses more resources, because it involves urging several extra objects, which always means using more resources at runtime and memory:

Iterator i = Arrays.asList(arreglo).iterator();
while(i.hasNext()){
    // trabajar con Objeto o = i.next()
}
    
answered by 13.04.2017 / 15:34
source
5

From java 8 you can use functional programming with Lambda expressions

Try to make the code as legible as possible.

//declaramos e inicializamos el arreglo de numeros int.
int [] arreglo = {5,7,8,3,12,45,95,2,56,789,123,456,789,14,1,3,45,84,984};

//for normal indicamos una variable i la que almacenara el indice del arreglo el cual vamos a iterar y se incrementara en uno.
for(int i=0;i<arreglo.length ; i++)
{
    System.out.println(arreglo[i]);
}

//utilizamos un for-each para escribir menos codigo, indicando el tipo de dato,una variable pivote luego dos puntos (:) y finalmente el arreglo que vamos a recorrer 
for(int pivote : arreglo)
{
    System.out.println(pivote);
}

//utilizamos Java 8 con Lambdas , primero convertimos nuestro arreglo a una lista con el metodo Arrays.asList luego utilizamos el metodo foreach de las colecciones Java el cual nos pide un parametro Consumer.
Arrays.asList(arreglo).forEach(System.out::println);

I hope it serves you Greetings.

    
answered by 13.04.2017 в 15:31
1

If you mean to traverse an array completely (what it would be to read all its elements) I really do not think that any optimization (or this being invaluable) can be applied beyond readability in one way or another, this is due that the compiler will end up doing the conversion of it.

I leave this question in the English version, with more complete answer in case it can be useful: link

    
answered by 13.04.2017 в 14:56