0

我必须将此输入从文件拆分为向量并添加到向量中。

文件输入

    1,375,seller
   1,375,sellers
   1,375,send
   1,375,sister
   1,375,south
   1,375,specific
   1,375,spoiler
   1,375,stamp
   1,375,state
   1,375,stop
   1,375,talked
   1,375,tenant
   1,375,today
   1,375,told

    FileInputStream fstream = new FileInputStream("e://inputfile.txt");
        // Use DataInputStream to read binary NOT text.
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;

        while ((strLine = br.readLine()) != null)  
        {
            Vector dataPoints = new Vector();
            dataPoints.add(br);


             dataPoints.add(new DataPoint());
        }


   ------ public DataPoint(double x, double y, String name) this is the method

如何将字符串拆分为 double 和 string 并为向量提供输入?

4

2 回答 2

3

使用String#split(String)

Vector<DataPoint> dataPoints = new Vector<DataPoint>();        
while ((strLine = br.readLine()) != null) {
    String[] array = strLine.split(",");
    dataPoints.add(new DataPoint(Double.parseDouble(array[0]), Double.parseDouble(array[1]), array[2]));
}
于 2013-02-13T16:14:30.683 回答
2

只需拆分Bufferedreader使用String.split()using,作为分隔符返回的字符串。并且还考虑使用 anArrayList而不是Vector,除非您关心线程安全并且还使您的集合具有通用性。

 Vector<DataPoint> dataPoints = new Vector<DataPoint>();   
 while ((strLine = br.readLine()) != null)  
        {
            String[] arr = strLine.split(",");
            DataPoint point = new DataPoint(Double.valueOf(arr[0]), Double.valueOf(arr[1]), arr[2]);
            dataPoints.add(point);
        }
于 2013-02-13T16:13:32.157 回答