【问题标题】:Iterating an Array List to calculate the percentage of population growth迭代一个数组列表来计算人口增长的百分比
【发布时间】:2016-10-16 00:35:08
【问题描述】:

我想创建一个方法来计算 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();



}



}

【问题讨论】:

    标签: java loops csv arraylist


    【解决方案1】:

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

    首先,将数据模型与控制器逻辑分开是个好主意。控制器处理业务逻辑,而数据模型存储数据并提供访问和更改数据的方法。您还应该适当地命名该类 - 使用名称 USCrimeClass 可以改进,因为很明显这是一个类,您不必在名称中使用“类”一词。您还应该为类成员变量创建 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);
        }
    
    }
    

    【讨论】:

    • 我明白你的意思。谢谢你的澄清。
    【解决方案2】:

    你可以像这样迭代列表:

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

    【讨论】:

    • 那么在我初始化 currentCrime 对象之后,我是否迭代到列表中的下一项,然后进行计算?我已经有一段时间没有使用数组列表了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    相关资源
    最近更新 更多