Java 在堆栈内部的ArrayList中搜索元素
我不知道这是否是最优的,但我必须为考试练习使用堆栈,我决定使用一个包含ArrayList的堆栈,其中ArrayList包含字符串类型变量 稍后我想寻找一个元素,一个ID。这就是我被卡住的地方。 这是我为这个搜索功能编写的代码:Java 在堆栈内部的ArrayList中搜索元素,java,arraylist,stack,Java,Arraylist,Stack,我不知道这是否是最优的,但我必须为考试练习使用堆栈,我决定使用一个包含ArrayList的堆栈,其中ArrayList包含字符串类型变量 稍后我想寻找一个元素,一个ID。这就是我被卡住的地方。 这是我为这个搜索功能编写的代码: private void btn_actualizarActionPerformed(java.awt.event.ActionEvent evt) { // TOD
private void btn_actualizarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//Busqueda:
String criterio;
Stack <ArrayList<ArrayList<String>>> pila_registro=new Stack <>(); //This is outside this class, added it here just so you know how the stack works
criterio=JOptionPane.showInputDialog("Ingrese el carne de seguro del paciente por buscar:");
int index=pila_registro.search(criterio);
if (index!= -1) {
JOptionPane.showMessageDialog(null,"Paciente encontrado!");
System.out.print(index);
}
else{
JOptionPane.showMessageDialog(null, "Paciente no encontrado!");
System.out.println(index);
}
}
private void btn\u actionperformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
//布斯奎达:
弦判据;
Stack pila_registro=new Stack();//这在这个类之外,添加到这里只是为了让您知道堆栈是如何工作的
criterio=JOptionPane.showInputDialog(“乘客车厢入口”);
int index=pila_registro.search(标准);
如果(索引!=-1){
showMessageDialog(null,“Paciente encontrado!”);
系统输出打印(索引);
}
否则{
showMessageDialog(null,“Paciente no encontrado!”);
系统输出打印项次(索引);
}
}
你在坚持什么?你在使用一堆二维数组列表。假设您的ID是字符串格式,暴力解决方案将是迭代堆栈中的每个元素,然后迭代每个外部ArrayList,然后迭代每个内部ArrayList,并查找与搜索的ID字符串相等的ID字符串。返回的值可能是堆栈中2d数组的位置+外部+内部ArrayList中ID的位置。