简体   繁体   English

Java-ArrayList-从数组数组获取值

[英]Java - ArrayList - Get a value from an Array of array

I save some values from database using this function (i've been replaced Vector, because is deprecated) : 我使用此功能从数据库保存了一些值(由于不推荐使用,我已替换为Vector):

// database function
public ArrayList<String[]> selectQuery(String query) {
    ArrayList<String[]> v = null;
    String [] record;
    int colonne = 0;
    try {
        Statement stmt = db.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        v = new ArrayList<String[]>();
        ResultSetMetaData rsmd = rs.getMetaData();
        colonne = rsmd.getColumnCount();

        while(rs.next()) {
            record=new String[colonne];
            for (int i=0; i<colonne; i++) record[i] = rs.getString(i+1);
            v.add((String[])record);
        }
        rs.close();
        stmt.close();
    } catch (Exception e) { e.printStackTrace(); errore = e.getMessage(); }

    return v;
}

// here i print the result, after call that function
ArrayList db_result=null;
db_result=mydb.selectQuery("SELECT titolo, user, date FROM articles WHERE category='1' ORDER by ID ASC");

int i=0;
while (i<db_result.size() ) {
    affitta_3.createLabel().setLabel(db_result.get(i)[0]);
    affitta_3.createLabel().setLabel(db_result.get(i)[1]);
    affitta_3.createLabel().setLabel(db_result.get(i)[2]);
    affitta_3.createLabel().setLabel(db_result.get(i)[3]);
   i++;
}

So, i save many String-array in an array. 因此,我将许多字符串数组保存在一个数组中。 Now, how can I get (for example) the 4° value from the 2° Array String? 现在,如何从2°数组字符串中获取(例如)4°值?

Why are you bothering to clone the string array when nothing else will have a reference to it? 当没有其他引用可以引用字符串数组时,为什么还要麻烦去克隆它呢?

Anyway, you'd get at the string using: 无论如何,您将使用以下命令获取字符串:

String value = v.get(1)[3];

(assuming that v is of type List<String[]> or something similar). (假设v的类型为List<String[]>或类似的类型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM