将数组添加到ArrayList

时间:2017-01-28 22:18:40

标签: java arrays loops oop arraylist

我是java的新手,但仍然遇到阵列问题。这里有任何帮助。我的程序从主类中的CSV输入文件中读取数据,并假设使用第二个类将每个行(数组)存储到ArrayList中,在该列表中,主类会将Array列表打印到屏幕上。我真的迷失了如何实际实现它。

示例输入

PERSON,ADD,BB222222,Anna

主要课程

    try{
        File inputFile = new File(INPUT_FILENAME);
        Scanner scanFile = new Scanner(inputFile);
        String aLine;
        String[]theLines;

        while(scanFile.hasNextLine()){

            aLine = scanFile.nextLine();
            theLines = aLine.split(",");
            if ("PERSON".equals(theLines[0]) && "ADD".equals(theLines[1])){
                processPersonAddition(theLines);
            }

将每一行添加到ArrayList

的方法
public static void processPersonAddition(String[]theLines){
    Person personAdd = new Person();
    setPersonAttributes(personAdd, theLines);
    personLogImpl.add(theLines);
    }

PersonLogImpl类

import java.util.ArrayList;

public class PersonLogImpl {

    private boolean add;
    private String licenseNumber, firstName
    private ArrayList<Person> myList;


    public ArrayList<Person> getPersonLog(){
        return myList;
    }

    public boolean add(Object obj){  //add person object to ordered list 
       ArrayList<String> list = new ArrayList<>();

       return add;
    }

这是第三类但是工作正常,只是用作getter和setter

编辑: 这是setPersonsAttributes方法

private static void setPersonAttributes(Person person, String[]inputLineValues){
    person.setLicenseNumber(inputLineValues[2]);
    person.setFirstName(inputLineValues[3]);
}

1 个答案:

答案 0 :(得分:1)

你快到了!

我建议进行以下更改:

将'processPersonAddition'方法更改为以下:\

1 Master/Driver Node : 
  Memory :24GB Cores :8 

4 Worker Nodes : 
  Memory :24GB Cores :8

spark.streaming.backpressure.enabled     true
spark.streaming.stopGracefullyOnShutdown true
spark.executor.instances                 28
spark.executor.memory                    2560MB
spark.executor.cores                     4
spark.driver.memory                      3G
spark.driver.cores                       1

更改“myList”的声明以初始化它。

<configuration> <!-- Site specific YARN configuration properties --> <property> <name>yarn.nodemanager.aux-services</name> <value>mapreduce_shuffle</value> </property> <property> <name>yarn.resourcemanager.hostname</name> <value>hdfs-name-node</value> </property> <property> <name>yarn.nodemanager.resource.memory-mb</name> <value>22528</value> </property> <property> <name>yarn.nodemanager.resource.cpu-vcores</name> <value>7</value> </property> <property> <name>yarn.scheduler.maximum-allocation-mb</name> <value>22528</value> </property> <property> <name>yarn.nodemanager.local-dirs</name> <value>file:///tmp/hadoop/data/nm-local-dir,file:///tmp/hadoop/data/nm-local-dir/filecache,file:///tmp/hadoop/data/nm-local-dir/usercache</value> </property> <property> <name>yarn.nodemanager.localizer.cache.cleanup.interval-ms</name> <value>500</value> </property> <property> <name>yarn.nodemanager.localizer.cache.target-size-mb</name> <value>512</value> </property> </configuration>

然后,将PersonImpl类中的add方法更改为以下:

 public static void processPersonAddition(String[]theLines){
    Person personAdd = new Person();
    setPersonAttributes(personAdd, theLines);
    personLogImpl.add(personAdd);
    }

稍后,每当您想要打印所有人时,只需遍历'myList'数组。