简体   繁体   English

Java 将数据从 2 数组列表复制到 2 维数组

[英]Java Copying data from 2 array list to a 2 dimensional array

Can anyone please share an example to store the resultset to a 2 dimensional array.谁能分享一个将结果集存储到二维数组的示例。 First column on the database table have dependency on second column.数据库表的第一列依赖于第二列。 I don't think array list will work if the columns have dependencies.如果列具有依赖关系,我认为数组列表不会起作用。

 ResultSet rs = stmt.executeQuery(sql);

 List a1 = new ArrayList();
 List a2 = new ArrayList();
 
// List<List> arraylist2D = new ArrayList<List>();
 
 
 

while(rs.next()){
    
    
    
    
     a1.add(rs.getString("ID"));
     a2.add(rs.getString("ID"));
     
     
    //Display values
    
     
}

Assigned to array list but need to copy to 2 dimensional array.分配给数组列表但需要复制到二维数组。 Thanks谢谢

NOTE: Your list initialization has no object class so it will have an error.注意:您的列表初始化没有 object class 所以它会出错。 Therefore, I'll assume you are handling Integers (It doesn't really matter though).因此,我假设您正在处理整数(但这并不重要)。

I'm not sure what you mean by your post as it isn't worded the best but I'll try my best.我不确定你的帖子是什么意思,因为它的措辞不是最好的,但我会尽力而为。 You need to initialize a 2d array with a width of the length of the largest ArrayList (unless they are the same, then it doesn't matter).您需要初始化一个宽度为最大 ArrayList 长度的二维数组(除非它们相同,否则没关系)。 Then, you fill it in. Here is the code:然后,你填写它。这是代码:

List<Integer> a1 = new ArrayList();
List<Integer> a2 = new ArrayList();
int[][] arr2d = new int[a1.size()][2/*two arraylists*/];

for (int i = 0; i < a1.size(); i++) {
    arr2d[i][0] = a1.get(i);
    arr2d[i][1] = a2.get(i);
}

This works flawlessly and without error.这完美无缺且没有错误。 Hope I helped!希望我有所帮助!

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

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