我想从csv文件中的单独列创建两个不同的数组。 csv文件有一列'月份'和一列'温度'。这是我迄今为止,但我不知道如何将这些值转换为数组。如何将.csv文件中的两个不同列转换为数组?
public class InputOutput
{
private double temperature;
public InputOutput()
{
List<String> temperatures = new ArrayList<String>();
Iterator<String> tempIterator = temperatures.iterator();
}
public static double getTemp()
{
double tempReading = 0.0;
try
{
String fileName = "C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv"; //location of MNMINPLS.csv
File file = new File(fileName);
Scanner scan = new Scanner(file); //scanner reads file
while(scan.hasNext()) //iterator
{
String tempData = scan.next();
String[] nums = tempData.split(",");
tempReading = Double.parseDouble(nums[3]);
System.out.println(tempReading);
}//end while
scan.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}//end try/catch
return tempReading;
}//end method getTemp
public static int getMonth()
{
int m = 0;
try
{
String fileName = "C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv"; //location of MNMINPLS.csv
File file = new File(fileName);
Scanner scan = new Scanner(file); //scanner reads file
while(scan.hasNext()) //iterator
{
String month = scan.next();
if(month == month)
m++;
String[] timeOfYear = month.split(",");
m = Integer.parseInt(timeOfYear[0]);
System.out.println(m);
}
scan.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}//end try/catch
return m;
}
}
你想阵列或数组列表(看你有一个温度数组列表)?无论何时您不知道如何使用标准的Java类,最好先参考[documentation](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList)的.html)。你会发现你可以在大部分时间回答你自己的问题。提示:ArrayList有一个.add()方法。 – MarsAtomic 2015-04-02 19:26:57