Remove brackets from an arraylist

1


I'm starting in this Java and I have a problem. I make a query in mysql and I save the data in an ArrayList, of which I only need the data, without the brackets that appear at the beginning and end.
Could someone help me by telling me how to remove those brackets?

    public class ArregloObservadas {

public ArrayList setArregloOBS_1(){

    PreparedStatement pstmt=null;
        ResultSet rs=null;
        ResultSetMetaData RSMD;
        Connection conn=null;

        ArrayList tmp3 = null;

        String sql2;

        conexion opendb = new conexion();

        try{    conn = opendb.getConnection();
        } catch(Exception e){System.out.println(e);}

        try{
            sql2 = "**consulta**";

            pstmt = conn.prepareStatement(sql2);

            rs   = pstmt.executeQuery();
            RSMD = rs.getMetaData();

            tmp3 = new ArrayList();  

            while (rs.next()){
                tmp3.add(rs.getInt("id_comision"));

            }

            if(rs!=null){
                rs.close();
                rs=null;
            }
            if(pstmt!=null){
                pstmt.close();
                pstmt = null;
            }
            if(conn!=null){
                conn.close();
                conn=null;
            }

        }catch(Exception e){
            System.out.println(e);
        }finally{
            try{
                if(rs!=null){
                rs.close();
                rs=null;
                }
                if(pstmt!=null){
                    pstmt.close();
                    pstmt = null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(Exception e){
                System.out.println(e);
            }
        }

        return tmp3;
    }

    public static void main(String[] args) {
        ArregloObservadas x = new ArregloObservadas();
        System.out.println(x.setArregloOBS_1());
    }
    
asked by Lía0512 21.06.2017 в 18:54
source

2 answers

6

The brackets you mention are shown because you are printing the representation of ArrayList , for example something similar to this:

[Valor1, Valor2, Valor3, Valor4, Valor5]

However, this should not mean a problem in terms of handling the data contained in ArrayList , but if your goal is to print the values, you can do it this way:

    String datosArray = "";
    for (String elemento: myArrayList) {
        datosArray += elemento + ", ";
    }
    System.out.println(datosArray);

add this method to clean the last , :

private static String limpia(String datosArray){
     datosArray = datosArray.trim();
     if (datosArray != null && datosArray.length() > 0 && datosArray.charAt(datosArray.length() - 1) == ',') {           
       datosArray = datosArray.substring(0, datosArray.length() - 1);              
     }
     return datosArray;
}   

and call it like this:

  System.out.println(limpia(datosArray));

in this way you would get:

Valor1, Valor2, Valor3, Valor4, Valor5

In the case of your code, it would be:

  public static void main(String[] args) {
        ArregloObservadas x = new ArregloObservadas();
        //System.out.println(x.setArregloOBS_1());

        String datosArray = "";
        for (String elemento : x.setArregloOBS_1()) {
            datosArray += elemento + ", ";
        }
        System.out.println(limpia(datosArray));

    }
    
answered by 21.06.2017 / 19:20
source
1

From Java 8 you can use the join method of the String class like this:

String listString = String.join(", ", list);

In case you use a version of java before 8, the other answer is appropriate.

    
answered by 22.06.2017 в 02:17