如何在Java中获取列表中的特定元素? [英] How to get to a particular element in a List in java?

查看:245
本文介绍了如何在Java中获取列表中的特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件.它有很多行.每行中都有多个用逗号分隔的值.

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

我正在使用OPENCSV从文件中提取数据.我希望能够直接转到List数据结构中的任何特定行.

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();
            }

此打印

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

我想知道是否仍然可以通过List访问各个行,元素.

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

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

我想要String [] nextLine = 1,2,3,4,5(其中nextLine [0] = 1 nextLine [1] = 2等等),在下一次迭代中,nextLine应该为6,7,8,9 ,10等

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

推荐答案

List接口支持通过get方法进行随机访问-要获取第0行,请使用list.get(0).您需要对此使用数组访问,即lines.get(0)[0]是第一行的第一元素.

The List interface supports random access via the get method - to get line 0, use list.get(0). You'll need to use array access on that, ie, lines.get(0)[0] is the first element of the first line.

此处.

这篇关于如何在Java中获取列表中的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆