如何让ArrayList打印出不同数据类型的信息?
How can I get an ArrayList to print out information of different data types?
我有一项任务,我必须按照平价医疗法案中符合条件的公民比例从高到低的顺序打印美国各州。我必须创建一个 ArrayList,它包含两种不同类型的数据:两个字符串和 3 个双精度数。我非常接近完成,但我被难住了:
如何逐行打印状态?我想我应该使用 t/ 来开始一个新行。
如何按百分比从高到低的顺序打印出各州?
到目前为止的代码。
import java.util.*;
import java.io.*;
import java.util.ArrayList;
// Calculates the percentage of citizens enrolled in the Affordable Healthcare Act
// Lists the states in order of highest percentage of enrolled citizens in the Affordable Healthcare Act
public class ACA {
public static void main( String[] args ) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in); // creates an object of the Scanner class
System.out.println("Enter filename"); // prompts the user for the name of the file
String filename = keyboard.next(); // the user response is stored
File inputFile = new File(filename+".txt"); // takes the filename and creates an object of the File class
Scanner fileIn = new Scanner(inputFile); // takes the object and converts the file to an obect of the Scanner class to allow it to be read
// creates an object of the ArrayList class
ArrayList<ACAdata> info = new ArrayList<>();
// variables declared for each column of data in the line
String state = " "; // State abbreviation
String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
double numEll = 0; // Number of citizens in the state eligible for ACA coverage
double numEn = 0; // Number of citizens who enrolled in an ACA plan
// while loop will evaluate as long as there are rows of data in the text file
while ( fileIn.hasNext() ) {
state = fileIn.next(); // individual state
org = fileIn.next(); // organization
numEll = fileIn.nextDouble(); // number of elligible citizens for that state for the Affordable Healthcare Act
numEn = fileIn.nextDouble(); // number of citizens enrolled in the Affordable Healthcare Act
double percentage = per( numEll, numEn ); // calls the per method to calculate a percentage for each state
// adds the 5 fields of data to a new ArrayList that holds the information for each state
info.add(new ACAdata( state, org, numEll, numEn, percentage));
}
// Prints out the information about the state
for ( int i = 0; i < info.size(); i++ ) {
System.out.println((info.get(i)).toString());
}
}
// method that finds the percentage of enrolled citizens that are elligible for the Affordable Care Act
public static double per( double Ell, double En ) {
double calculation = En / Ell * 100; // divides the Enrolled by the number of elligible citizens
return calculation; // returns the calculation
}
}
public class ACAdata {
// variables declared for each column of data in the line
String state = " "; // State abbreviation
String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
double numEll = 0; // Number of citizens in the state eligible for ACA coverage
double numEn = 0; // Number of citizens who enrolled in an ACA plan
double percentage = 0;
public ACAdata ( String state, String org, double numEll, double numEn, double percentage ) {
state = state;
org = org;
numEll = numEll;
numEn = numEn;
percentage = percentage;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getOrg() {
return this.org;
}
public void setOrg( String org ) {
this.org = org;
}
public double getNumEll() {
return this.numEll;
}
public void setNumEll( double numEll ) {
this.numEll = numEll;
}
public double getNumEn( double numEn ) {
return this.numEn;
}
public double getPercentage() {
return this.percentage;
}
public void setPercentage( double percentage ) {
this.percentage = percentage;
}
}
看看你的代码,最简单的解决方案(我能想到的)是用类似的东西覆盖 ACAdata
中的 toString
(按照你想要的方式添加和格式化)
@Override
public String toString() {
return String.format("%s %s %.2f %.2f", state, org, numEll, numEn);
}
然后您可以打印整个 ArrayList
或单个 ACAdata
实例。您已经在调用 toString()
,但您不必... Java 会在您尝试打印 Object
时隐式为您调用 toString()
( Object
中的默认实现并没有按照您的要求进行。
使 ACA 数据对象可排序。
如果你有这个对象,你可以对它进行排序,然后输出它。
ArrayList<ACAdata> list = new ArrayList<>();
Collections.sort(list);
for (Iterator<ACAdata> iterator = list.iterator(); iterator.hasNext();) {
ACAdata next = iterator.next();
System.out.println(next.toString());
}
public class ACAdata implements Comparable<ACAdata> {
// variables declared for each column of data in the line
String state = " "; // State abbreviation
String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
double numEll = 0; // Number of citizens in the state eligible for ACA coverage
double numEn = 0; // Number of citizens who enrolled in an ACA plan
double percentage = 0;
public ACAdata(String state, String org, double numEll, double numEn, double percentage) {
state = state;
org = org;
numEll = numEll;
numEn = numEn;
percentage = percentage;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getOrg() {
return this.org;
}
public void setOrg(String org) {
this.org = org;
}
public double getNumEll() {
return this.numEll;
}
public void setNumEll(double numEll) {
this.numEll = numEll;
}
public double getNumEn(double numEn) {
return this.numEn;
}
public double getPercentage() {
return this.percentage;
}
public void setPercentage(double percentage) {
this.percentage = percentage;
}
@Override
public int compareTo(ACAdata o) {
if (percentage > o.percentage) {
return -1;
}
if (percentage < o.percentage) {
return 1;
}
return 0;
}
@Override
public String toString() {
return "Put the info you want here!";
}
}
我有一项任务,我必须按照平价医疗法案中符合条件的公民比例从高到低的顺序打印美国各州。我必须创建一个 ArrayList,它包含两种不同类型的数据:两个字符串和 3 个双精度数。我非常接近完成,但我被难住了:
如何逐行打印状态?我想我应该使用 t/ 来开始一个新行。
如何按百分比从高到低的顺序打印出各州?
到目前为止的代码。
import java.util.*;
import java.io.*;
import java.util.ArrayList;
// Calculates the percentage of citizens enrolled in the Affordable Healthcare Act
// Lists the states in order of highest percentage of enrolled citizens in the Affordable Healthcare Act
public class ACA {
public static void main( String[] args ) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in); // creates an object of the Scanner class
System.out.println("Enter filename"); // prompts the user for the name of the file
String filename = keyboard.next(); // the user response is stored
File inputFile = new File(filename+".txt"); // takes the filename and creates an object of the File class
Scanner fileIn = new Scanner(inputFile); // takes the object and converts the file to an obect of the Scanner class to allow it to be read
// creates an object of the ArrayList class
ArrayList<ACAdata> info = new ArrayList<>();
// variables declared for each column of data in the line
String state = " "; // State abbreviation
String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
double numEll = 0; // Number of citizens in the state eligible for ACA coverage
double numEn = 0; // Number of citizens who enrolled in an ACA plan
// while loop will evaluate as long as there are rows of data in the text file
while ( fileIn.hasNext() ) {
state = fileIn.next(); // individual state
org = fileIn.next(); // organization
numEll = fileIn.nextDouble(); // number of elligible citizens for that state for the Affordable Healthcare Act
numEn = fileIn.nextDouble(); // number of citizens enrolled in the Affordable Healthcare Act
double percentage = per( numEll, numEn ); // calls the per method to calculate a percentage for each state
// adds the 5 fields of data to a new ArrayList that holds the information for each state
info.add(new ACAdata( state, org, numEll, numEn, percentage));
}
// Prints out the information about the state
for ( int i = 0; i < info.size(); i++ ) {
System.out.println((info.get(i)).toString());
}
}
// method that finds the percentage of enrolled citizens that are elligible for the Affordable Care Act
public static double per( double Ell, double En ) {
double calculation = En / Ell * 100; // divides the Enrolled by the number of elligible citizens
return calculation; // returns the calculation
}
}
public class ACAdata {
// variables declared for each column of data in the line
String state = " "; // State abbreviation
String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
double numEll = 0; // Number of citizens in the state eligible for ACA coverage
double numEn = 0; // Number of citizens who enrolled in an ACA plan
double percentage = 0;
public ACAdata ( String state, String org, double numEll, double numEn, double percentage ) {
state = state;
org = org;
numEll = numEll;
numEn = numEn;
percentage = percentage;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getOrg() {
return this.org;
}
public void setOrg( String org ) {
this.org = org;
}
public double getNumEll() {
return this.numEll;
}
public void setNumEll( double numEll ) {
this.numEll = numEll;
}
public double getNumEn( double numEn ) {
return this.numEn;
}
public double getPercentage() {
return this.percentage;
}
public void setPercentage( double percentage ) {
this.percentage = percentage;
}
}
看看你的代码,最简单的解决方案(我能想到的)是用类似的东西覆盖 ACAdata
中的 toString
(按照你想要的方式添加和格式化)
@Override
public String toString() {
return String.format("%s %s %.2f %.2f", state, org, numEll, numEn);
}
然后您可以打印整个 ArrayList
或单个 ACAdata
实例。您已经在调用 toString()
,但您不必... Java 会在您尝试打印 Object
时隐式为您调用 toString()
( Object
中的默认实现并没有按照您的要求进行。
使 ACA 数据对象可排序。 如果你有这个对象,你可以对它进行排序,然后输出它。
ArrayList<ACAdata> list = new ArrayList<>();
Collections.sort(list);
for (Iterator<ACAdata> iterator = list.iterator(); iterator.hasNext();) {
ACAdata next = iterator.next();
System.out.println(next.toString());
}
public class ACAdata implements Comparable<ACAdata> {
// variables declared for each column of data in the line
String state = " "; // State abbreviation
String org = " "; // Organizer of the ACA marketplace, either FFM for federally facilitated marketplace or SBM for state based marketplace.
double numEll = 0; // Number of citizens in the state eligible for ACA coverage
double numEn = 0; // Number of citizens who enrolled in an ACA plan
double percentage = 0;
public ACAdata(String state, String org, double numEll, double numEn, double percentage) {
state = state;
org = org;
numEll = numEll;
numEn = numEn;
percentage = percentage;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getOrg() {
return this.org;
}
public void setOrg(String org) {
this.org = org;
}
public double getNumEll() {
return this.numEll;
}
public void setNumEll(double numEll) {
this.numEll = numEll;
}
public double getNumEn(double numEn) {
return this.numEn;
}
public double getPercentage() {
return this.percentage;
}
public void setPercentage(double percentage) {
this.percentage = percentage;
}
@Override
public int compareTo(ACAdata o) {
if (percentage > o.percentage) {
return -1;
}
if (percentage < o.percentage) {
return 1;
}
return 0;
}
@Override
public String toString() {
return "Put the info you want here!";
}
}