Arraylist打印修剪或拆分字符串

时间:2019-06-19 15:09:09

标签: java arraylist

我已经从数据库中获取数据并将结果集放在Array-list中。 我需要从数组列表中打印每个可以拆分或修剪为字符串的字符串。 以下代码和期望结果的输出。

public static ArrayList<Object> myList = new ArrayList<Object>();   
public ArrayList<Object> databaseconection(WebDriver driver, String sqlquery)
        throws InterruptedException, SQLException {

    Statement stmt = null;
    Connection conn = null;
    DriverManager.registerDriver(new SybDriver());
    Thread.sleep(1000);
    String url = "***************";
    conn = DriverManager.getConnection(url, "451552", "Welcome"     conn.setAutoCommit(false);
    stmt = conn.createStatement();
    // Database query to get value
    ResultSet rs = stmt.executeQuery(sqlquery);

    while (rs.next()) {
        for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
            // System.out.print(" " + rs.getMetaData().getColumnName(i) + "=" +
            // rs.getObject(i));
            // System.out.println("");
            myList.add(rs.getObject(i));
        }
        System.out.println(myList);
        //  round off to nearest million
        for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
            Object object2 = rs.getObject(i);
            object2 = ((BigDecimal) object2).divide(new BigDecimal("1000000")).setScale(0, RoundingMode.HALF_DOWN)
                    + "";
            System.out.println(object2);

            // Print each Array from my list
            Add_Log.info(myList.toString() + " round off to nearest million = " + object2);
            Reporter.log(myList.toString() + " round off to nearest million = " + object2);
        }
    }
    return myList;

}

上面代码的控制台结果。

[1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436]
1995538
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =1995538
1131933
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =1131933
1174448
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =1174448
39942
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =39942
2224235
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =2224235
2940469
INFO [main] (BaselPage.java:114) - [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] round off to nearest million =2940469

我希望每行输出

 [1995537811893.93004, 1131932531051.9879252, 1174448486100.98669, 39942430884.26804, 2224235008605.54787, 2940469028187.27436] mylist Arraylist
  ///// Each line 
[1995537811893.93004] round off to nearest million =1995538
[1131932531051.9879252] round off to nearest million = 1131933
[1174448486100.98669] round off to nearest million = 1174448
[39942430884.26804] round off to nearest million = 39942
[2224235008605.54787] round off to nearest million = 2224235
[2940469028187.27436] round off to nearest million = 2940469

2 个答案:

答案 0 :(得分:1)

您每次都将整个列表打印为字符串。改用mylist.get(i)(这里不需要toString())。

答案 1 :(得分:0)

ResultSet rs = stmt.executeQuery(sqlquery);

        while (rs.next()) {
            for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
                // System.out.print(" " + rs.getMetaData().getColumnName(i) + "=" +
                // rs.getObject(i));
                // System.out.println("");
                myList.add(rs.getObject(i));
            }
            System.out.println(myList);
            //  round off to nearest million

                for (int i1 = 0; i1 < myList.size(); i1++) {
                // Print each Array from my list
                    myListdec = myList.get(i1);
                Add_Log.info(myList.get(i1) + " round off to nearest million = " +  ((BigDecimal) myListdec).divide(new BigDecimal("1000000")).setScale(0, RoundingMode.HALF_DOWN)+ "");
                Reporter.log(myList.get(i1) + " round off to nearest million = " +  ((BigDecimal) myListdec).divide(new BigDecimal("1000000")).setScale(0, RoundingMode.HALF_DOWN)+ "");

                }
相关问题