长风秋雁
显然可以使用数组来完成这类事情,但是没有简单的方法可以知道您可能正在读取的文件中包含多少数据行,除非您当然知道您将要读取的特定数据文件将始终包含相同数量的数据线。许多程序员将使用 ArrayList 或 List 接口对象来保存数据,如果仍然需要一个类型的数组,则该集合将转换为该类型的数组。填充二维数组:首先,您需要知道数据文件中实际包含多少有效的文件行数据。最简单的方法是进行两次读取。第一遍计算文件中有多少有效数据行,以便正确确定数组大小,然后第二遍填充二维数组。有一些简单的方法可以确定文件可能包含的行数,但没有简单的方法可以确定它们是否是有效的数据行(实际包含数据的行)。您的特定数据文件似乎包含三个特定部分:Header部分(文件的第一行);原始数据部分(文件正文);和需求部分(文件的最后一行)。将每个部分检索到单独的数组中可能是有益的,因此我在下面提供的代码演示了如何实现这一点。首先,您需要建立 3 个类成员变量,它们将具有整个类的全局范围:String[] headerData; // First data Line in file is the Header InformationObject[][] data; // The 2D Object Array to hold raw File Dataint[] demandData; // Last data line in file is the Demand Information现在将以下方法复制/粘贴到您的类中:private void readDataFile(String filePath) { Scanner sc; int maxColumns = 0; int validLines = 0; int endLine; try { sc = new Scanner(new File(filePath)); String dataLine = ""; // First Pass: while (sc.hasNextLine()) { String fileLine = sc.nextLine().trim(); /* Skip Blank Lines or lines that start with a comment character (if any)... */ if (fileLine.equals("") || fileLine.startsWith(";") || fileLine.startsWith("#")) { continue; } validLines++; //This line must be a valid data line, so increment counter. dataLine = fileLine; if (validLines == 1) { // Get Header Information. headerData = dataLine.split("\\s+"); // Grab the Columns Header Names maxColumns = headerData.length; // Get the number of data columns } } endLine = validLines; demandData = new int[endLine - 2]; // Initialize the demandData array data = new Object[endLine - 2][maxColumns]; // Initialize the data array // Make sure the last line actually contains data if (dataLine.equals("")) { // No it doesn't so there is no DEMAND line. System.out.println("No DEMAND Data Available In File!"); sc.close(); return; } /* Fill the demandData int array with Integer data... The dataLine variable at this point will hold the last valid file data line which happend to be the Demand Line */ String[] dl = dataLine.split("\\s+"); // split the data line on 1 or more whitespaces. // Don't want the word "DEMAND" so we start from 1. for (int i = 1; i < dl.length; i++) { demandData[i-1] = Integer.parseInt(dl[i]); } // Second Pass (fill the data aray): sc = new Scanner(new File("MyDataFile.txt")); validLines = 0; while (sc.hasNextLine()) { String fileLine = sc.nextLine().trim(); /* Skip Blank Lines or lines that start with a comment character (if any)... */ if (fileLine.equals("") || fileLine.startsWith(";") || fileLine.startsWith("#")) { continue; } validLines++; //This line must be a valid data line, so increment counter. if (validLines == endLine) { break; } if (validLines == 1) { continue; } // Skip the header line dl = fileLine.split("\\s+"); // split the data line on 1 or more whitespaces. for (int i = 0; i < dl.length; i++) { if (i == 0) { data[validLines-2][i] = dl[i]; //String Object - Sources Name } else { data[validLines-2][i] = Integer.parseInt(dl[i]); // Integer Object } } } sc.close(); /* Close the reader. Make sure the number of data rows equals the number of Demand values otherwise inform of mismatch. */ if (data.length != demandData.length) { System.out.println("Warning: There is missing DEMAND information (data mismatch)!\n" + "There is no available Demand data for one of the Sources."); } } catch (FileNotFoundException ex) { System.err.println(ex.getMessage()); }}以下是您可以如何使用此方法:readDataFile("10by10.txt");if (data != null) { // Header Information: System.out.println("Header Column Names:\n" + Arrays.toString(headerData) + "\n"); // The Actual Data: System.out.println("File Data:"); for (int i = 0; i < data.length; i++) { System.out.println(Arrays.toString(data[i])); } // The Demand Information: System.out.println("\nDemand Values For Each Row:\n" + Arrays.toString(demandData) + "\n");}如果您在此处提供示例文件数据,则控制台窗口将显示以下内容:Header Column Names:[COSTS, A, B, C, D, E, F, G, H, I, J, SUPPLY]File Data:[Source1, 6, 7, 10, 16, 5, 8, 15, 15, 6, 8, 175][Source2, 10, 14, 8, 17, 13, 9, 18, 20, 9, 7, 200][Source3, 9, 4, 8, 12, 10, 10, 8, 5, 9, 10, 225][Source4, 12, 8, 9, 10, 6, 15, 4, 9, 7, 0, 300][Source5, 6, 9, 17, 7, 6, 13, 6, 7, 6, 0, 250][Source6, 9, 10, 9, 13, 9, 8, 9, 3, 4, 9, 100][Source7, 16, 18, 7, 14, 5, 6, 10, 5, 4, 5, 150][Source8, 7, 5, 8, 3, 8, 5, 10, 8, 8, 14, 300][Source9, 8, 10, 9, 6, 4, 9, 17, 7, 5, 8, 100][Source10, 5, 8, 4, 5, 7, 14, 6, 3, 13, 9, 200]Demand Values For Each Data Row:[150, 250, 110, 275, 175, 350, 300, 180, 90, 120]填充二维列表界面:在这里,我们做一些不同的事情,你会看到它可以变得多么容易。这次我们的readDataFile()方法返回一个 String 类型的 2D List Interface 对象,我们可以轻松地操作该对象来获取 Header Column Names、Raw Data 和 Demand Data 值。这是方法: private List<List<String>> readDataFile(String filePath) { Scanner sc; List<List<String>> list = new ArrayList<>(); try { sc = new Scanner(new File(filePath)); while (sc.hasNextLine()) { String fileLine = sc.nextLine().trim(); /* Skip Blank Lines or lines that start with a comment character (if any)... */ if (fileLine.equals("") || fileLine.startsWith(";") || fileLine.startsWith("#")) { continue; } /* Valid data line we want...add it to the list. Here we split the data line into a String array then we use the Arrays.asList() method to convert it to a List then we add that List to our 2D List. All done in one line of code. */ list.add(Arrays.asList(fileLine.split("\\s+"))); } sc.close(); // Close the reader. } catch (FileNotFoundException ex) { System.err.println(ex.getMessage()); } return list;}并针对您的示例数据文件使用此方法:// Read the file data and place it all into a 2D ListList<List<String>> list = readDataFile("10by10.txt");// Get the Header Column Names from first ArrayListString[] headerData = list.get(0).toArray(new String[0]); // The 2D Object Array to hold raw File DataObject[][] data = new Object[list.size() - 2][headerData.length]; for(int i = 1; i < list.size() - 1; i++) { for (int j = 0; j < list.get(i).size(); j++) { if (j == 0) { data[i - 1][j] = list.get(i).get(j); } else { data[i - 1][j] = Integer.parseInt(list.get(i).get(j)); } }}// Last data line in file is the DEMAND ValuesString[] tmp = list.get(list.size() - 1).toArray(new String[0]);int[] demandData = new int[tmp.length - 1];for (int i = 1; i < tmp.length; i++) { demandData[i - 1] = Integer.parseInt(tmp[i]);}if (data != null) { // Header Information: System.out.println("Header Column Names:\n" + Arrays.toString(headerData) + "\n"); // The Actual Data: System.out.println("File Data:"); for (int i = 0; i < data.length; i++) { System.out.println(Arrays.toString(data[i])); } // The Demand Information: System.out.println("\nDemand Values For Each Row:\n" + Arrays.toString(demandData) + "\n");}控制台窗口的输出将与上面显示的相同。