打开csv集合和连续数组

发布于 2024-11-17 03:25:59 字数 767 浏览 5 评论 0原文

我不太明白 opencsv 网站上给出的关于如何在此处使用集合列表的信息,例如:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));**
List myEntries = reader.readAll();**

我尝试访问列表的成员,例如 System.out.println(myEntries.get(2)) 但它给了我类似 d1e604 的东西,当我测试元素是否存在时,

boolean b = myEntries.contains("the element");** and it returns false.

我需要做什么来访问标记化字符串?

通过使用 readNext(),元素由“\n”分隔,我想以连续的编号分配元素。的数组。

while((tokened = reader.readNext()) != null){
    int numOfArray = tokened.length;
    System.out.println("number of items:"+ numOfArray) ;
    for (int i = 0;i < numOfArray;i++) {
    System.out.println("     tokenNo[" + i + "]:  " + tokened[i]);

   }
 }

i dont quite understand by the given eg on opencsv site on how to use collection here List,eg is:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));**
List myEntries = reader.readAll();**

I tried accessing a member of the List for eg System.out.println(myEntries.get(2)) but it gives me something like d1e604 and when i tested for existence of an element

boolean b = myEntries.contains("the element");** and it returns false.

what do i need to do to access the tokenized Strings?

by using readNext(), the elements are separated by "\n" and i want to assign the elements in a continuous no. of array.

while((tokened = reader.readNext()) != null){
    int numOfArray = tokened.length;
    System.out.println("number of items:"+ numOfArray) ;
    for (int i = 0;i < numOfArray;i++) {
    System.out.println("     tokenNo[" + i + "]:  " + tokened[i]);

   }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-11-24 03:25:59

我认为您错过了有关 CSV 的一些事实。 CSV 是关于行和列的。 readNext() 返回表示一行的字符串数组。所以我猜想,readAll()返回一个string[]列表。

I think you missed a little fact about CSV. CSV is about lines AND columns. readNext() returns an array of strings representing a line. So I would guess, readAll() returns a list of string[].

我的痛♀有谁懂 2024-11-24 03:25:59

List myEntries 的每个元素都是一个 String[]。
即这是一个字符串数组。
这意味着你需要演员阵容。

String[] entry = (String[]) myEntries.get(2);

另外 -

System.out.println(myEntries.get(2)) but it gives me something like d1e604.

那是因为 toString 方法没有被正确覆盖。这是 Object 类中实现的 toString 方法的默认行为。

Each element of your List myEntries is a String[].
i.e. That is an array of String.
that means you need a cast.

String[] entry = (String[]) myEntries.get(2);

Also -

System.out.println(myEntries.get(2)) but it gives me something like d1e604.

That's because the toString method is not properly overridden. That's the default behavior of the toString method as implemented in the Object class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文