迭代数组列表以计算人口增长的百分比

时间:2016-10-16 00:35:08

标签: java loops csv arraylist

我想创建一种方法来计算1994 - 2013年人口增长的百分比变化,并打印出每个百分比的变化。我有所有存储的数据,但我不知道如何迭代ArrayList来完成这个

import java.io.*;
import java.util.*;
public class USCrimeClass 
{

public int year;
public int population;
public int violentCrime;
public double violentCrimeRate;
public int manslaughter;
public double manslaughterRate;
public int rape;
public double rapeRate;
public int robbery;
public double robberyRate;
public int assault;
public double assaultRate;
public int propertyCrime;
public double propertyCrimeRate;
public int burglary;
public double burglaryRate;
public int larcenyTheft;
public double larcenyTheftRate;
public int vehicleTheft;
public double vehicleTheftRate;

public USCrimeClass(String line)
{
    String[]split=line.split(",");
    year=Integer.parseInt(split[0]);
    population=Integer.parseInt(split[1]);
    violentCrime=Integer.parseInt(split[2]);
    violentCrimeRate=Double.parseDouble(split[3]);
    manslaughter=Integer.parseInt(split[4]);
    manslaughterRate=Double.parseDouble(split[5]);
    rape=Integer.parseInt(split[6]);
    rapeRate=Double.parseDouble(split[7]);
    robbery=Integer.parseInt(split[8]);
    robberyRate=Double.parseDouble(split[9]);
    assault=Integer.parseInt(split[10]);
    assaultRate=Double.parseDouble(split[11]);
    propertyCrime=Integer.parseInt(split[12]);
    propertyCrimeRate=Double.parseDouble(split[13]);
    burglary=Integer.parseInt(split[14]);
    burglaryRate=Double.parseDouble(split[15]);
    larcenyTheft=Integer.parseInt(split[16]);
    larcenyTheftRate=Double.parseDouble(split[17]);
    vehicleTheft=Integer.parseInt(split[18]);
    vehicleTheftRate=Double.parseDouble(split[19]);
}



Scanner read = null;
{
try
{
    read=new Scanner(new File("C:\\Crime.csv"));
}
catch(FileNotFoundException e)
{
    System.out.println("The file can't be opened");
    System.exit(0);
}
List<USCrimeClass> crimeClasses = new ArrayList<>();
read.nextLine();
while(read.hasNextLine())
{
    crimeClasses.add(new USCrimeClass(read.nextLine()));
}

read.close();



}



}

2 个答案:

答案 0 :(得分:1)

我要指出的一些项目是更多的代码审查项目,这些项目与您尝试执行的业务逻辑没有特别相关,但是,从长远来看,您的代码将更具可读性并且可维护。

首先,将数据模型与控制器逻辑分开是个好主意。控制器处理业务逻辑,而数据模型存储数据并提供访问和更改数据的方法。您还应该适当地命名该类 - 使用名称USCrimeClass可以得到改进,因为很明显这是一个类,您不必在名称中使用“class”一词。您还应该为类成员变量创建getter和setter方法,而不是允许直接访问。在下面的代码示例中,我为population字段创建了一个getter和setter对作为示例。

public class USCrimeData {

    private int year;
    private int population;
    private int violentCrime;
    private double violentCrimeRate;
    private int manslaughter;
    private double manslaughterRate;
    private int rape;
    private double rapeRate;
    private int robbery;
    private double robberyRate;
    private int assault;
    private double assaultRate;
    private int propertyCrime;
    private double propertyCrimeRate;
    private int burglary;
    private double burglaryRate;
    private int larcenyTheft;
    private double larcenyTheftRate;
    private int vehicleTheft;
    private double vehicleTheftRate;

    public USCrimeData(String line) {
        String[] split = line.split(",");
        year = Integer.parseInt(split[0]);
        population = Integer.parseInt(split[1]);
        violentCrime = Integer.parseInt(split[2]);
        violentCrimeRate = Double.parseDouble(split[3]);
        manslaughter = Integer.parseInt(split[4]);
        manslaughterRate = Double.parseDouble(split[5]);
        rape = Integer.parseInt(split[6]);
        rapeRate = Double.parseDouble(split[7]);
        robbery = Integer.parseInt(split[8]);
        robberyRate = Double.parseDouble(split[9]);
        assault = Integer.parseInt(split[10]);
        assaultRate = Double.parseDouble(split[11]);
        propertyCrime = Integer.parseInt(split[12]);
        propertyCrimeRate = Double.parseDouble(split[13]);
        burglary = Integer.parseInt(split[14]);
        burglaryRate = Double.parseDouble(split[15]);
        larcenyTheft = Integer.parseInt(split[16]);
        larcenyTheftRate = Double.parseDouble(split[17]);
        vehicleTheft = Integer.parseInt(split[18]);
        vehicleTheftRate = Double.parseDouble(split[19]);
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }
}

接下来让我们谈谈控制器。这是业务逻辑所在的位置。您可以在此处使用数据模型来获得所需的结果。由于您要确定人口变化百分比,您将使用犯罪数据列表来获取1994年的人口(我假设这是列表中的第一个条目 - 索引0),然后获取列表中的最后一个条目(我假设最后的条目是2013)然后计算你想要的值。如果这些假设不正确或者您想计算不同年份的增长,您可能希望在数据模型中实现getYear()方法,然后循环遍历数据列表,直到找到具有所需年份的对象为止

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Controller {

    public static void main(String[] args) {
        Scanner read = null;

        try {
            // Replace the "..." below with actual path to your data file.
            read = new Scanner(new File("..."));
        } catch (FileNotFoundException e) {
            System.out.println("The file can't be opened");
            System.exit(0);
        }
        List<USCrimeData> crimeClasses = new ArrayList<>();

        while (read.hasNextLine()) {
            crimeClasses.add(new USCrimeData(read.nextLine()));
        }

        read.close();

        int initialPopulation = crimeClasses.get(0).getPopulation();
        System.out.println("initialPopulation: "+initialPopulation);
        int latestPopulation = crimeClasses.get(crimeClasses.size()-1).getPopulation();
        System.out.println("latestPopulation: "+latestPopulation);
        double percentGrowth = (double)(latestPopulation - initialPopulation) / initialPopulation * 100;

        System.out.println("percentGrowth: "+percentGrowth);
    }

}

答案 1 :(得分:0)

您可以迭代列表:

Iterator<USCrimeClass> iterator = crimeClasses.iterator();
while(iterator.hasNext()) {
  USCrimeClass currentCrime = iterator.next();
  // use currentCrime object to calcuate percent increase/decrease.
}