字符串数组列表本身覆盖 [英] List of String Arrays overwriting itself

查看:135
本文介绍了字符串数组列表本身覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题在这里,完整的新手所以尽量放轻松我。我试图找出什么是错在这里:

First ever question here, complete newbie so try to take it easy on me. I'm trying to figure out what's wrong here:

public List<String[]> idOnlyQuery(String searchTerm, Connection conn){

    List<String[]> result = new ArrayList<String[]>();
    String[] rowResult = new String[7];

    Statement stmt = null;      
    try {
      stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery( "SELECT * FROM Jets WHERE CurrentID LIKE '" + searchTerm + "%'");
      while(rs.next()){

          String currentId= rs.getString("CurrentId");
          String manufacturer = rs.getString("Constructor");
          String type = rs.getString("ACType");
          String series = rs.getString("Series");
          String index = rs.getString("KeyNo");
          String operator = rs.getString("Operator");
          String baseAirport = rs.getString("Home_Airfield");
          rowResult[0] = (currentId);
          rowResult[1] = (manufacturer);
          rowResult[2] = (type);
          rowResult[3] = (series);
          rowResult[4] = (operator);
          rowResult[5] = (baseAirport);
          rowResult[6]= (index);
          result.add(rowResult);
      }
    System.out.println(result.get(0)[0]);

该方法返回的字符串数组列表,这些阵列是从SQLite数据库检索。一切从事实,在我的结果列表的字符串数组似乎越来越重写每次while循环重演时间相隔工作。最后一个的println 总是给我的 currentId 从数据库中检索到的最后一排,但如果我把的println ,而循环返回每个周期的同时适当的数据里。我真的无法揣摩出我的的String [] 元素被覆盖掉了。这也可能是真的很明显,但我花了几个小时网上找来检查,如果我正确,还在做着添加过程没有喜悦。帮助!

This method returns a list of string arrays, these arrays are retrieved from an SQLite database. Everything works apart from the fact that the string arrays in my result List seem to be getting overwritten every time the while loop repeats itself. The last println always gives me the currentId of the last row retrieved from the database, but if I put a println inside the while loop it returns the appropriate data on each while cycle. I really can't figure out where my String[] elements are getting overwritten. It's probably really obvious but I've spent hours looking online to check if I'm doing the add procedure correctly and still no joy. HELP!

推荐答案

您需要创建回路中 rowResult 阵列。您的列表只是持有指向数组和你基本上只能用一个实例工作的所有的时间。

You need to create the rowResult array in the loop. Your list just holds a pointer to the array and you are basically only working with one instance all the time.

这篇关于字符串数组列表本身覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆