读取文本文件的第一行并将其存储到数组列表中

huangapple 未分类评论50阅读模式
英文:

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&lt;String&gt; nodes = new ArrayList&lt;String&gt;();
	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(&quot;,&quot;);
				for(int i =0; i &lt; 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?

huangapple
  • 本文由 发表于 2020年4月11日 00:18:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61144322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定