英文:
Read the first line of a text file and store it into an array list
问题
public class WeightedGraph {
public int matrix;
public String node;
public int array[][];
public ArrayList<String> nodes = new ArrayList<String>();
public static int NEGATIVE = -1;
private String[] record;
public WeightedGraph(String infile){
try {
File newFile = new File(infile);
Scanner scan = new Scanner(newFile);
while (scan.hasNext()) {
String firstLine = scan.nextLine();
record = firstLine.split(",");
for(int i = 0; i < record.length; i++) {
nodes.add(record[i]);
}
}
(Note: The given text appears to be a Java code snippet for creating a weighted graph and reading data from a file. I have provided the translated code portion as requested, but please let me know if you need further assistance or have any questions related to the code.)
英文:
public class WeightedGraph {
public int matrix;
public String node;
public int array[][];
public ArrayList<String> nodes = new ArrayList<String>();
public static int NEGATIVE = -1;
private String[] record;
public WeightedGraph(String infile){
try {
File newFile = new File(infile);
Scanner scan = new Scanner(newFile);
while (scan.hasNext()) {
String firstLine = scan.nextLine();
record = firstLine.split(",");
for(int i =0; i < record.length; i++) {
nodes.add(record[i])
}
}
I am trying to build a weighted adjacency matrix from a text file, but the first task I must do is read in the first line. I tried this method but I keep running into errors trying to add the elements of the record array into the nodes array list. Can anyone give me a simple solution to fix this?
专注分享java语言的经验与见解,让所有开发者获益!
评论