尝试获取Java中ArrayList的输出

时间:2018-11-20 23:12:03

标签: java arraylist

我正在尝试获取一个将csv文件放入到ArrayList中并显示ArrayList内容的代码。我相信我有正确的代码可以将数据放入ArrayList中,但无法打印。请任何建议,欢迎。我对Java和编码一般还是很陌生。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

class Employee{
    public String FirstName;
    public String LastName;
    public String Company;
    public String Address;
    public String City;
    public String County;
    public String State;
    public String Zip;
    public String Phone;
    public String Fax;
    public String Email;
    public String Web;    
}       

public class JTeagueITCO321IPWeek4 {
    public static void main(String[] args) throws FileNotFoundException, IOException {

        String line = "";
        ArrayList <Employee> ALEmployee = new ArrayList();

        FileReader fr = new FileReader("C:\\Users\\User\\Downloads\\ITCO321_U4IP_sample_data.csv");
        try (BufferedReader br = new BufferedReader(fr)) {
            while ((line = br.readLine())!=null){ 
                Employee emp = new Employee();
                String[] empFields = line.split(",");
                emp.FirstName = empFields[0];
                emp.LastName = empFields[1];
                emp.Company = empFields[2];
                emp.Address = empFields[3];
                emp.City = empFields[4];
                emp.County = empFields[5];
                emp.State = empFields[6];
                emp.Zip = empFields[7];
                emp.Phone = empFields[8];
                emp.Fax = empFields[9];
                emp.Email = empFields[10];
                emp.Web = empFields[11];
                ALEmployee.add(emp);
            }
        }
    }
} 

2 个答案:

答案 0 :(得分:1)

要打印ArrayList,您只需

  • 使用System.out.println(ALEmployee);

  • 或更精确地说,逐个打印每个元素

    for(Employee e : ALEmployee){
        System.out.println(e);
    }
    

在两种情况下,您都需要将toString()类中的Employee方法实现为:

public String toString(){
    return FirstName+" "+LastName+" "+Company; // do whatever you want here
}

我建议您遵循Java约定和编码约定:

  • 使用lowerCamelCase命名属性/变量
  • 将属性设置为private,并使用setters或构造函数来实例化Employee及其属性

答案 1 :(得分:0)

这是我想出的代码。我将就如何清理此问题征求建议和批评。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
class Employee {
        public String FirstName;
        public String LastName;
        public String Company;
        public String Address;
        public String City;
        public String County;
        public String State;
        public String Zip;
        public String Phone;
        public String Fax;
        public String Email;
        public String Web;

public String toString(){
return "FirstName+" "+LastName+" "+Company+" "+Address+" "+City+""+County+"
        +State+" "+Zip+" "+Phone+" "+Phone+" "+Fax+" "+Email+" "+Web;

}
}       
/**
*
* @author 
*/
public class  {
/**
 * @param args the command line arguments
 * @throws java.io.FileNotFoundException
 */
public static void main(String[] args) throws FileNotFoundException, IOException {

         String line = "";
         ArrayList <Employee> ALEmployee = new ArrayList<Employee>();

    FileReader fr = new FileReader("C:\\Users\\User\\Downloads\\ITCO321_U4IP_sample_data.csv");
    try (BufferedReader br = new BufferedReader(fr)) {
        while ((line = br.readLine())!=null){ 
            Employee emp = new Employee();
            String[] empFields = line.split(",");
            emp.FirstName = empFields[0];
            emp.LastName = empFields[1];
            emp.Company = empFields[2];
            emp.Address = empFields[3];
            emp.City = empFields[4];
            emp.County = empFields[5];
            emp.State = empFields[6];
            emp.Zip = empFields[7];
            emp.Phone = empFields[8];
            emp.Fax = empFields[9];
            emp.Email = empFields[10];
            emp.Web = empFields[11];
        ALEmployee.add(emp);          
    }        
    }
    ALEmployee.forEach((e) -> {
        System.out.println(e);
    });


    }
 }