开发者

open csv collection and continuous array

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]);

   }
 }


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[].


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜