[英]How to Load a file in parallel arrays in java
我有一個包含以下格式的 30 行數據的文件:
月日年氣價
以下是一些示例數據:
1994 年 5 月 2 日 1.04
有人能告訴我如何在 Java 中以月、天、價格的並行數組加載這個文件嗎? 在此之后,我將不得不顯示最低價格、最高價格以及每個月的平均價格。
對於那些想知道的人:
並行數組是其中每個數組中的數據與任何給定記錄數據行直接相關的數組。 一個數組的索引包含另一個數組中相同索引處的相關數據。 例如:
Dimension[] dim = new Dimension[4];
dim[0] = new Dimension(1,11);
dim[1] = new Dimension(2,22);
dim[2] = new Dimension(3,33);
dim[3] = new Dimension(4,44);
int[] widths = {dim[0].width, dim[1].width, dim[2].width, dim[3].width};
int[] heights = {dim[0].height, dim[1].height, dim[2].height, dim[3].height};
for (int i = 0; i < widths.length; i++) {
int w = widths[i];
int h = heights[i];
System.out.println("Width: " + w + " | Height: " + h);
}
控制台輸出將是:
Width: 1 | Height: 11
Width: 2 | Height: 22
Width: 3 | Height: 33
Width: 4 | Height: 44
寬度和高度整數數組被認為是並行數組,因為每個數組中的數據索引直接相關並相互平行。 您迭代哪個並行數組並不重要,當前迭代與所有其他並行數組相關。
您很快就會明白為什么將帶有成員變量的類用於此類事情而不是數組要好得多,尤其是當涉及多個並行數組時,但是,它有一些實際用途。
手頭的任務:
當您讀入文件時,您希望使用String#split()方法將該行分成所需的塊。 此處用於 split() 方法的分隔符將是空格 ( " " ) 或正則表達式(RegEx) "\\\\s+" ,例如:
"This is my String".split("\\s+");
split()方法中的 RegEx 參數基本上意味着,將字符串拆分為一個或多個空格。
在下面的示例中,並行數組是類成員變量。 使用文件中的相關數據填充這些數組的方法名為fillParallelArraysWithFileData() ,它接受一個參數,即數據文件的路徑和名稱。 這是代碼:
private static String[] monthsArray;
private static int[] daysArray;
private static int[] yearsArray;
private static double[] pricesArray;
public static void fillParallelArrayWithFileData(final String filePath) {
Scanner read = null;
try {
read = new Scanner(new File(filePath));
/* First Read Pass
===============
Get the number of VALID data lines in file.
We need this count to know how large to size
our Parallel Arrays.
*/
int lineCount = 0; // counter
while (read.hasNextLine()) {
String line = read.nextLine().trim(); // Trim lead/trailing whitespaces (if any)
/* Skip past blank or comment lines. Lines that start with
a semicolon (;) or a hash character (#) are considered
comment lines here and are ignored. You can get rid of
those conditions if you like. */
if (line.equals("") || line.startsWith(";") || line.startsWith("#")) {
continue;
}
lineCount++; // Increment the counter.
}
/* Second Read Pass
================
Get the file data and fill Arrays...
Declare Arrays which will be our parallel arrays. */
monthsArray = new String[lineCount];
daysArray = new int[lineCount];
yearsArray = new int[lineCount];
pricesArray = new double[lineCount];
int indexIncrementer = 0;
// Start the read from beginning again...
read = new Scanner(new File(filePath));
while (read.hasNextLine()) {
String line = read.nextLine();
// Remove the comma in data line. Don't want it.
line = line.trim().replace(",", "");
// If the current line is blank or a comment then skip past it.
if (line.equals("") || line.startsWith(";") || line.startsWith("#")) {
continue;
}
// Split the current data line
String[] lineParts = line.split("\\s+");
monthsArray[indexIncrementer] = lineParts[0];
daysArray[indexIncrementer] = Integer.parseInt(lineParts[1]);
yearsArray[indexIncrementer] = Integer.parseInt(lineParts[2]);
pricesArray[indexIncrementer] = Double.parseDouble(lineParts[3]);
indexIncrementer++;
}
}
catch (FileNotFoundException ex) {
System.out.println("FILE NOT FOUND! [" + filePath + "]");
}
finally {
if (read != null) {
read.close();
}
}
}
一個示例用法可能是:
// Fill Arrays with File data.
fillParallelArrayWithFileData("GasPrices.txt");
// Get Gas price for month of July in 1994
String desiredMonth = "July";
int desiredYear = 1994;
// We could iterate through any one of the Parallel Arrays
for (int i = 0; i < pricesArray.length; i++) {
if (monthsArray[i].equalsIgnoreCase(desiredMonth) && yearsArray[i] == desiredYear) {
String m = "Date: " + monthsArray[i] + " ";
String d = daysArray[i] + ", ";
String y = yearsArray[i] + " - ";
String p = "Gas Price: $" + pricesArray[i];
System.out.println(m + d + y + p);
}
}
輸出到控制台窗口將類似於:
Date: July 2, 1994 - Gas Price: $1.12
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.