[java] How to get to a particular element in a List in java?

I have a CSV file. It has many lines. In each line there are multiple values separated by commas.

I am using OPENCSV to extract data from the file. I want to have the ability to directly go to any particular line which is in List data structure.

CSVReader reader = new CSVReader(new FileReader(
                    "myfile.csv"));
            try {
                List<String[]> myEntries = reader.readAll();
                for (String[] s : myEntries) {
                    System.out.println("Next item: " + s);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

This prints

Next item: [Ljava.lang.String;@6c8b058b
Next item: [Ljava.lang.String;@1b192059
Next item: [Ljava.lang.String;@e9ac0f5
Next item: [Ljava.lang.String;@51f21c50
Next item: [Ljava.lang.String;@6e20f237
Next item: [Ljava.lang.String;@34fe315d
Next item: [Ljava.lang.String;@1c5aebd9
Next item: [Ljava.lang.String;@1532021a
Next item: [Ljava.lang.String;@62803d5
Next item: [Ljava.lang.String;@2d13981b
Next item: [Ljava.lang.String;@61672bbb

I want to know if there is anyway I could access individual lines, elements via List.

i.e

    1,2,3,4,5
    6,7,8,9,10
    11,12,13,14
    15,16,17,18

I want String[] nextLine = 1,2,3,4,5 (where nextLine[0] = 1 nextLine[1] = 2 etc) and in the next iteration nextLine should be 6,7,8,9,10 etc

This question is related to java list

The answer is


String[] is an array of Strings. Such an array is internally a class. Like all classes that don't explicitly extend some other class, it extends Object implicitly. The method toString() of class Object, by default, gives you the representation you see: the class name, followed by @, followed by the hash code in hex. Since the String[] class doesn't override the toString() method, you get that as a result.

Create some method that outputs the array elements for you. Iterate over the array and use System.out.print() (not print*ln*) on the elements.


At this point:

for (String[] s : myEntries) {
   System.out.println("Next item: " + s);
}

You need to join the array of Strings in a line. Check this post: A method to reverse effect of java String.split()?


CSVReader reader = new CSVReader(new FileReader(
                "myfile.csv"));
        try {
            List<String[]> myEntries = reader.readAll();
            for (String[] s : myEntries) {
               for(String ss : s) {
                   System.out.print(", " + ss);
               }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

Check out the split function of String Here, and use it something like this String[] results = input.split("\\s+");. The regular expresion bit spilts on whitespaces, it should do the trick


The toString method of array types in Java isn't particularly meaningful, other than telling you what that is an array of.

You can use java.util.Arrays.toString for that.

Or if your lines only contain numbers, and you want a line as 1,2,3,4... instead of [1, 2, 3, ...], you can use:

java.util.Arrays.toString(someArray).replaceAll("\\]| |\\[","")

You have about 98% right. The only issue is that you're trying to print out an String[] which does not print the way you'd like. Instead try this...

for (String[] s : myEntries) {
    System.out.print("Next item: " + s[0]);
    for(int i = 1; i < s.length; i++) {
        System.out.print(", " + s[i]);
    }
    System.out.println("");
}

This way you're accessing each string in the array instead of the array itself.

Hope this helps!