Lista de matrices get(int index) se utiliza para recuperar un elemento de la lista. Debemos especificar el índice al llamar al método get y devuelve el valor presente en el índice especificado.

public Element get(int index)

Este método genera IndexOutOfBoundsException si el índice es menor que cero o mayor que el tamaño de la lista (índice <0 OR indice> = tamaño de la lista).

Ejemplo

En el siguiente ejemplo, obtenemos algunos elementos de una lista de matrices utilizando el método get.

package beginnersbook.com;
import java.util.ArrayList;
public class GetMethodExample {
   public static void main(String[] args) {
       ArrayList<String> al = new ArrayList<String>();
       al.add("pen");
       al.add("pencil");
       al.add("ink");
       al.add("notebook");
       al.add("book");
       al.add("books");
       al.add("paper");
       al.add("white board");

       System.out.println("First element of the ArrayList: "+al.get(0));
       System.out.println("Third element of the ArrayList: "+al.get(2));
       System.out.println("Sixth element of the ArrayList: "+al.get(5));
       System.out.println("Fourth element of the ArrayList: "+al.get(3));
   }
}

Producción:

First element of the ArrayList: pen
Third element of the ArrayList: ink
Sixth element of the ArrayList: books
Fourth element of the ArrayList: notebook

Referencia:

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#get(int)

leer  Cómo convertir un vector en una matriz de cadenas en java

Por avivcas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *