如何将所选项目从一个数组复制到另一个数组?

时间:2013-10-21 23:57:32

标签: java arrays

有没有办法从另一个创建一个数组?在我的情况下我有一个列表,这个列表有复选框,每一行,直到现在我可以得到复选框的值检查。现在我的问题是:我可以获取这些复选框值并从中创建另一个数组吗?

if (accion.equalsIgnoreCase("agregar")) {
  List<Directorio> listaDirectorio = DirectorioDAO.getListDirectorio(request.getParameter("a_selectCta"));
  request.setAttribute("a_listaDirectorio", listaDirectorio);               
  String select[] = request.getParameterValues("a_checkCta");
  for (int i = 0; i < select.length; i++) {
   directorio = DirectorioDAO.getDirectorio(Integer.parseInt(select[i]));   
   System.out.println(select[i]);   
   System.out.println(directorio);
  }             
 request.setAttribute("a_accion","agregar");
}

getDirectorio方法:

public static Directorio getDirectorio(int idDirectorio) {
 Connection connection = null;
 PreparedStatement statement = null;
 ResultSet rs = null;
 Directorio directorio=null;            
 try {
  connection = ConnectionDBM.getConnection();
  if (connection != null) {
   String sql = " SELECT id_directorio, id_ctahabiente, nombre_completo,cargo, calle_numero, "
              + " colonia, delegacion, CP, estado, telefono1, telefono2, ext1, ext2, correo, "
              + " observaciones, tipo, confirmado, principal, fechaModificacion "
              + " FROM DIRECTORIO WHERE id_directorio = ? ";
   statement = connection.prepareStatement(sql);
   statement.setInt(1, idDirectorio);
   rs = statement.executeQuery();
   if(rs.next()) {
    directorio= new Directorio();
    directorio.setIdDirectorio(rs.getInt("id_directorio"));
    directorio.setIdCtahabiente(rs.getString("id_ctahabiente"));
    directorio.setNombreCompleto(rs.getString("nombre_completo"));
    directorio.setCargo(rs.getString("cargo"));
    directorio.setCalleNumero(rs.getString("calle_numero"));
    directorio.setColonia(rs.getString("colonia"));
    directorio.setDelegacion(rs.getString("delegacion"));
    directorio.setCP(rs.getString("CP"));
    directorio.setEstado(rs.getString("estado"));
    directorio.setTelefono1(rs.getString("telefono1"));
    directorio.setTelefono2(rs.getString("telefono2"));
    directorio.setExt1(rs.getString("ext1"));
    directorio.setExt2(rs.getString("ext2"));
    directorio.setCorreo(rs.getString("correo"));
    directorio.setObservaciones(rs.getString("observaciones"));
    directorio.setTipo(rs.getString("tipo"));
    directorio.setConfirmado(rs.getBoolean("confirmado"));
    directorio.setPrincipal(rs.getBoolean("principal"));
    directorio.setFechaModificacion(rs.getDate("fechaModificacion"));       
   }
  }
 } catch (SQLException e) {
    logger.error("getRegControl: ", e);
    throw new RuntimeException(e);
 } catch (Exception e) {
    logger.error("getRegControl: ", e);
    throw new RuntimeException(e);
 } finally {
    try {
statement.close();
    } catch (Exception e) {
   }
   try {
    rs.close();
   } catch (Exception e) {  }
   try {
    if (connection != null && !connection.isClosed())
    connection.close();
   } catch (Exception e) {}
   }
  return directorio;    
}

1 个答案:

答案 0 :(得分:0)

在回答您的问题时,您可以使用收藏并使用addAll方法或使用System.arrayCopy

但说实话,为什么不改变您的查询,以便在其中查询所有选定的ID。

SELECT id_directorio, id_ctahabiente, nombre_completo,cargo, calle_numero, "
          + " colonia, delegacion, CP, estado, telefono1, telefono2, ext1, ext2, correo, "
          + " observaciones, tipo, confirmado, principal, fechaModificacion "
          + " FROM DIRECTORIO WHERE id_directorio  IN ? 

然后,您将加快查询速度并获得更清晰的代码。

请参阅https://stackoverflow.com/questions/13254133/jdbc-prepared-statement-how-to-set-a-list