gpt4 book ai didi

java - 将对象的 ArrayList 转换为二维数组

转载 作者:行者123 更新时间:2023-12-01 14:47:36 25 4
gpt4 key购买 nike

我在将对象的 Arraylist 转换为二维数组时遇到了一些麻烦。 arrayList ar 包含许多名为 Row 的对象。每行包含多个字符串。我希望能够遍历 arrayList,访问每一行并将字符串存储在数组中。如果这没有意义,我真的很抱歉。我什至觉得很难解释!这是我到目前为止所拥有的:

public String tableArray[][]; //Array of every applicant
private ArrayList<Row> ar;
private Row r;

public void display()
{
int i, j;
for (Row r: ar)
i = 0;
for(String s: r)
{
tableArray = new String[i][j];
j++;
}
i++;
}

这是 Row 类

import java.util.ArrayList;
import java.lang.String;

public class Row
{
private String appNumber;
private String name;
private String date;
private String fileLoc;
private String country;
private String elementString;
public String results[];

public Row(String appNumber, String name, String date, String fileLoc, String country, Table table)
{
this.appNumber = appNumber;
this.name = name;
this.date = date;
this.fileLoc = fileLoc;
this.country = country;
asAString();
table.addApplicant(this);

}

public void asAString()
{
String elementString = appNumber +","+ name +","+ date +","+ fileLoc +","+ country;
results = elementString.split(",");}
}

谢谢大家的帮助

最佳答案

final int listSize = list.size();
E[][] darr = new E[listSize][];
for(int i = 0; i < listSize; i++) {
ArrayList<E> sublist = list.get(i);
final int sublistSize = sublist.size();
darr[i] = new E[sublistSize];
for(int j = 0; j < sublistSize; j++) {
darr[i][j] = sublist.get(j);
}
}

我找到了这个答案here .

关于java - 将对象的 ArrayList 转换为二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15258426/

25 4 0