English 中文(简体)
查阅Java的ArrayList<ArrayList>有限档案
原标题:Read Tab Delimited file into ArrayList<ArrayList> in Java
  • 时间:2012-04-04 18:22:03
  •  标签:
  • java

我正在寻找一种办法,将一个已划定的基球雕成一个双层阵列清单。 我用扫描器读取文档,但我无法想到如何将一线仅读到一个阵列清单中,在线上停下来,然后将下一个线改为下一个阵列。

这是我第一次尝试制定一个多层面清单,我认为这同阅读多层面阵列一样。 我显然错了。

    public static ArrayList dataRead(String fileloc) throws FileNotFoundException {
    ArrayList array = new ArrayList<ArrayList>();
    Scanner s = new Scanner(new File(fileloc)).useDelimiter("	");
    ArrayList<String> rows = new ArrayList<String>();
    ArrayList cols = new ArrayList();
    while(s.nextLine() != null) {
        cols.add(s.next());
    }
    return array;
}

这是我的法典。 将每一行改为由回归划定的扼杀装置,然后将每一行体改为适当的阵列,这是否是一个更好的选择?

最佳回答

您可使用 开放式csv,并将直径确定为表格。 举例说明我提供的联系。

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"),  	 );
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

虽然从您的提问中看不出你的实际问题是什么?

问题回答

我不敢肯定统计所看的是什么,但最好使用哈希姆比,而后者则具有关键/价值,但我可能是错误的。 我不知道你的数据集。

For a start, you can delimit the lines by using the " " escape character. For Example:

Scanner s = new Scanner(input).useDelimiter(" ");

那么,你就可以通过结果,每把结果添加到地图上。

I think you need to rethink your data structure to something more conducive to what you are trying to store. I would recommend that you create a player object and store that into an arraylist.

public class Player{
    double battavg;
    String name;
    //add more values like rbi, etc.
    public Player(name,battavg){
        this.name=name;
        this.battavg=battavg;
    }
    public String getName(){
        return name;
    }
    public String getBattAvg(){
        return battavg;
    }
    public setBattAvg(double battavg){
        this.battavg=battavg;
    }
    public setName(String name){
        this.name=name;
    }
}

public class baseball{
    public static void main(String[] args){
        ArrayList<Player> list = new ArrayList<Player>();
        //read in values from csv
        list.add(new Player(name,battavg));
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签