我想從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