Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 2, 2019 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/27d96366962049e561a13de1bce46a33 to your computer and use it in GitHub Desktop.
Save parzibyte/27d96366962049e561a13de1bce46a33 to your computer and use it in GitHub Desktop.
/*
Buscar elemento en ArrayList de Java
https://parzibyte.me/blog
*/
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<String> canciones = new ArrayList<>();
// Le agregamos datos
canciones.add("Beds are burning");
canciones.add("It's only rock 'n' Roll (But I like it)");
canciones.add("Ashes to ashes");
String busqueda = "Ashes to ashes";
int indice = canciones.indexOf(busqueda);
if (indice != -1) {
System.out.println("La búsqueda está en el índice " + indice);
} else {
System.out.println("El elemento no existe");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment