遍历统计就很有可能创建出一个差不多和数据库一样大的内存空间,这样就是十分糟糕的情况。
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import com.sun.org.apache.regexp.internal.recompile; public class Dao { String findRyan() { String db_url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8"; String db_user = "root"; String db_password = ""; Connection conn = null; Statement state = null; ResultSet rs = null; ArrayList<String> list = new ArrayList<>(); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_password); state = conn.createStatement(); rs = state.executeQuery("select uid,reid from post"); while (rs.next()) { String idList = rs.getString("uid") + "|" + rs.getString("reid"); if (!(idList).equals("")) { for (String id : idList.split("\|")) { list.add(id); } } } ; } catch (Exception e) { e.printStackTrace(); } System.out.println(list); for (int i = 0; i < list.size() - 1; i++) { if (!list.get(i).equals(list.get(i + 1))) { list.set(i, "a"); list.set(i + 1, "a"); i=i+1; } } System.out.println(list); return null; } public static void main(String[] args) { Dao aDao = new Dao(); aDao.findRyan(); } }
于是有了一个取巧的方式,就是两两消去,利用“水王”有着过半数量的特性