【发布时间】:2020-10-04 08:03:13
【问题描述】:
我有一个 Java 程序,它从文件中读取元素,将它们存储在二维数组中,然后通过提交多个操作来操作它们。
我已经使用二维数组实现了程序,现在我想找到一种方法将这个数组变成二维数组列表,这样我就可以单独操作这些元素,就像我对二维数组所做的那样。
程序从如下所示的文件中读取数据:
Jason,56
Martha,89
James,23
...
这是我尝试将二维数组转换为二维数组列表的代码:
请记住,我希望将所有名称存储在数组/ArrayList 的第一列中,并将年龄存储在第二列中:
public class Testr {
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner sc = new Scanner(new BufferedReader(new FileReader("C:\\Users\\test.csv")));
int num_rows = countLines("C:\\Users\\test.csv");
System.out.println("Num of rows : " + num_rows);
int num_cols = countColumns("C:\\Users\\test.csv");
System.out.println("Num of cols : " + num_cols);
String[][] Entries_arr = new String[num_rows][num_cols];
while(sc.hasNextLine())
{
for(int i = 0; i < Entries_arr.length; i++)
{
String[] line;
line = sc.nextLine().trim().split(";");
for(int j = 0; j < line.length; j++)
{
Entries_arr[i][j] = line[j];
}
}
}
List<List<String>> Entries = new ArrayList<List<String>>();
for(int i = 0; i < Entries_arr.length; i++)
{
List<String> recs = new ArrayList<String>();
for(int j = 0; j < Entries_arr[i].length; j++)
{
recs.add(String.valueOf(Entries_arr[i][j]));
}
Entries.add(recs);
}
System.out.println(Entries);
}
//------------------------------------------------------------------------------------------------------------------------
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
//------------------------------------------------------------------------------------------------------
public static int countColumns(String filename) {
File file = new File(filename);
Scanner scanner;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
return -1;
}
int number = 0;
if (scanner.hasNextLine()) {
number = scanner.nextLine().split(";").length;
}
scanner.close();
return number;
}
}
感谢任何帮助。
【问题讨论】:
-
那么问题出在哪里?除了阅读之外,我没有看到你写信给
Entries_arr。 -
你的输出
Entries有什么问题? -
您不应该使用 2D 数组或 2D 列表,因为无论尝试解决方案,您都将使用并行数组或并行列表,这两者都会招致灾难。最好创建一个名为
User的类,它在私有字段中同时保存名称 String 和年龄 int,然后创建一个实例化为ArrayList<User>的单个List<user>,然后用这个单维列表填充用户对象。 -
您的代码似乎也过于复杂了。您应该简单地创建您的列表:
List<User> users = new ArrayList<>();然后遍历文件,从每一行创建用户对象并填充您的列表,并从每一行创建一个用户。无需创建数组,无需转换,一举搞定。 -
@DontKnowMuchButGettingBetter 谢谢你的回答,你能解释一下吗?我想做的就是像处理二维数组一样操作二维数组列表。因为这样我得到的每个条目都是这样的:[Jason,56],...
标签: java arrays string file arraylist