![](/img/trans.png)
[英]Accessing index values from an ArrayList and storing them in a seperate new ArrayList
[英]Storing array and accessing them
这是我用来从“ GeneID”列中检索数据的编码。 使用此编码,我可以在“ GeneID”列下打印所有名称。 但是我想存储列中的每个名称,以分别访问这些名称。 谁能帮我这个?
String name1[] = new String[100];
int i = 0;
String query = "select distinct GeneID from gene1";
PreparedStatement pest = connection.prepareStatement(query);
ResultSet rs = pest.executeQuery();
while (rs.next()) {
name1[i] = rs.getString("GeneID");
System.out.println(name1[i]);
}
rs.close();
pest.close();
对于使用阵列就得增加i
的内侧while (rs.next()) {
String name1[] = new String[100];
int i = 0;
String query = "select distinct GeneID from gene1";
PreparedStatement pest = connection.prepareStatement(query);
ResultSet rs = pest.executeQuery();
while (rs.next()) {
name1[i] = rs.getString("GeneID");
System.out.println(name1[i]);
i++;
}
rs.close();
pest.close();
}
为了进行访问,您可以遍历数组
for(int i;i<name1.length-1;i++){
System.out.println("Id is "+name1[i]);
}
或者你可以使用
for (String id: name1) {
System.out.println("Id is "+name1[i]);
}
您还可以使用ArrayList如下存储变量
ArrayList ar=new ArrayList();
String query = "select distinct GeneID from gene1";
PreparedStatement pest = connection.prepareStatement(query);
ResultSet rs = pest.executeQuery();
while (rs.next()) {
String id = rs.getString("GeneID");
ar.add(id);
}
rs.close();
pest.close();
}
用于遍历ArrayList
for (int i = 0; i < ar.size(); i++) {
System.out.println("Id is "+ar.get(i));
}
要么
for (String id : ar) {
System.out.println("Id is "+ar.get(i));
}
使用arrayList,以便可以使用.add()方法,这被证明很有帮助
List ls=new ArrayList();
ResultSet rsp=pss.executeQuery("select * from student");
while(rsp.next()){
ls.add(rsp.getString("your_column_name"));
}
rsp.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.